Giter VIP home page Giter VIP logo

Comments (20)

samayo avatar samayo commented on June 2, 2024

This is strange ... let me check. Also can I see your code?

from bulletproof.

samayo avatar samayo commented on June 2, 2024

I'm not getting this error.

from bulletproof.

elieobeid7 avatar elieobeid7 commented on June 2, 2024

This is my code running on Xampp 7.x on Fedora

<?php
require 'database_connect.php';
$image = new Bulletproof\Image($_FILES);
$imageName= base64_encode($_SESSION['admin_email']);         


$image->setName($imageName)
->setMime(array('jpeg', 'gif', 'png', 'jpg'))
->setLocation(__DIR__ . "/avatars"); 

if (isset($_POST['submit'])){

    $image->setDimension(128,128); 
    
    
    if($image["pictures"]){
  
        
        // set the max width/height limit of images to upload (limit in pixels)
        
        
        // pass name (and optional chmod) to create folder for storage
        $upload = $image->upload(); 
        
        if($upload){
            $photo = $upload->getFullPath(); // uploads/cat.gif
        }else{
            $photo = NULL;
        }
    }



        $fname = $_POST['fname'];
        $lname = $_POST['lname'];
        $email = $_POST['email'];

        $db->user[$_SESSION['admin_id']] = [
            'firstName' => $fname,
            'lastName' => $lname,
            'email' => $email,
            'photo' => $photo
        ];

} 



/* header("Location:profile.php");
exit; */
?>

I tried to remove setName altogether, didn't work

Please forgive my mess, I'm trying to upload a photo, then I'll clean it. getFullPath returns empty, upon checking why I receive this error. I'm using version 3.0.2.

also another error with setDimentions, I cannot do

$image->setName($imageName)
->setMime(array('jpeg', 'gif', 'png', 'jpg'))
->setLocation(__DIR__ . "/avatars")
->setDimension(128,28); 

it gives something like error boolean function, something like that forgot the error

from bulletproof.

samayo avatar samayo commented on June 2, 2024

Okay, at first glance this I was able to spot the issue. $_POST and $_FILES are two different arrays. One checked for posts made via your form (ex, text inputs) and the other $_FILES checks for file uploads. So, it is correct to see "undefined index tmp_name`... error, because you are not checking if form is submitted.

Anyway, I can't be sure right now because I am trying to find a fix to resolve the error that appears when the browser blocks the image upload due to max file constraint. If you leave that from your HTML it might work nicely, until I find a solution. For now, I was able to try this script, which is slightly modified version of your and it works as intended:

require __DIR__ . '/src/bulletproof.php'; 


$image = new Bulletproof\Image($_FILES);

$imageName = rand(1, 100) . '-foo';

$image->setName($imageName)
      ->setMime(array('jpeg', 'gif', 'png', 'jpg'))
      ->setLocation(__DIR__ . "/avatars")
      ->setDimension(128,128); 
    
    
    if($image["pictures"]){
        $upload = $image->upload(); 
        
        if($upload){
            $photo = $upload->getFullPath(); // uploads/cat.gif
            // store in db
        }else{
          echo "error while uploading " . $image['error'];
            $photo = NULL;
        }



}else{
  if($image['error']){
    var_dump($image['error']);
  }
}

Let me know if you have any more questions

from bulletproof.

samayo avatar samayo commented on June 2, 2024

Also make sure your HTML form element has the multiplart/form-data attribute eg: <form method="POST" enctype="multipart/form-data">

from bulletproof.

elieobeid7 avatar elieobeid7 commented on June 2, 2024

I just tried your code as is

$image->setName($imageName)
      ->setMime(array('jpeg', 'gif', 'png', 'jpg'))
      ->setLocation(__DIR__ . "/avatars")
      ->setDimension(128,128); 
    

And the dimension error i was talking about showed up

Fatal error: Uncaught Error: Call to a member function setDimension() on boolean in /opt/lampp/htdocs/web/emall/production/app/profile_ctrl.php:11 Stack trace: #0 {main} thrown in /opt/lampp/htdocs/web/emall/production/app/profile_ctrl.php on line 11

from bulletproof.

samayo avatar samayo commented on June 2, 2024

Well, maybe that is the issue. setLocation method can only return false (boolean) only if it failed to create a dir so, maybe you have no permission. In which case, you'll be able to capture the using your $image['error'] method

from bulletproof.

elieobeid7 avatar elieobeid7 commented on June 2, 2024

now it works, is it possible to resize the image?

from bulletproof.

samayo avatar samayo commented on June 2, 2024

Yes, just include one of the functions in the folder /utils which is for resizing images. They should work better than the actual bulletproof library

from bulletproof.

elieobeid7 avatar elieobeid7 commented on June 2, 2024

perfect thank you, since setDimensions is annoying, better to resize it if it doesn't fit. That's why setdimensions was giving me errors, I thought it automatically resize. My bad :)

from bulletproof.

elieobeid7 avatar elieobeid7 commented on June 2, 2024

One last question, if I let it do the naming automatically, can it guarantee unique names?

from bulletproof.

samayo avatar samayo commented on June 2, 2024

Yes, it will generate unique name always. Also setDimention method is for setting a limit of image width/height not resizing

from bulletproof.

elieobeid7 avatar elieobeid7 commented on June 2, 2024

I'm uploading like this

$photo = $upload->getName() . '.' . $upload->getMime();

and displaying like this, once they get to the db, since I cannot store getFullpath()

$path = 'avatars/';
$avatar =$path . $user->photo;

Is there a way to get the relative path? I mean is there a method I didn't use in your library that makes me write less code? it's nothing I know but when I saw getFullPath I started wondering about getRelativePath or similar

from bulletproof.

samayo avatar samayo commented on June 2, 2024

getFullPath() will return the path starting from where the image is stored in ex: if you upload in images:

$image->setLocation(__DIR__ . '/images') you will get the current path plus the full path before. If you want want to get only the relative path I think you should set the location as $image->setLocation('images')

from bulletproof.

elieobeid7 avatar elieobeid7 commented on June 2, 2024

I can't get the image from

<input type="file" name="data[photo]" id="photo" class="custom-file-input">

it always gives an undefined variable, I tried the script above and some other variations nothing worked

from bulletproof.

samayo avatar samayo commented on June 2, 2024

I think that may be because you are trying to upload multiple files. I haven't implemented that yet. You can just do a php foreach() and use the upload method to upload the images for now. Let me know if you are facing some issues to upload a single file

from bulletproof.

elieobeid7 avatar elieobeid7 commented on June 2, 2024

I'm just uploading one file, here's the latest variation I tried

<? require('common/session.php');
require "common/vendor/samayo/bulletproof/src/utils/func.image-resize.php";
$image = new Bulletproof\Image($_FILES);
if (isset($_POST['submit'])){


$image->setMime(array('jpeg', 'gif', 'png', 'jpg'))
      ->setLocation(__DIR__ . "/avatars");
    
    
    if($image[$data["photo"]]){
    
        
        $upload = $image->upload(); 
        
        if($upload){
            $resize = Bulletproof\resize(
                $image->getFullPath(), 
                $image->getMime(),
                $image->getWidth(),
                $image->getHeight(),
                120,
                120
         );
            $data['photo'] = $upload->getName() . '.' . $upload->getMime();    

        }else{
          echo "error while uploading " . $image['error'];
            $data['photo'] = NULL;
        }



} else{
  if($image['error']){
    var_dump($image['error']);
  }

  
} 



/* $data = array($_POST['data']);
    $db->user->insert()
    ->data($data)
    ->run(); */

    echo $data['photo'];
}



I tried many other things, this is my second project using your library, the first one worked, but now I had to use array since I have a billion input fields. faster to insert them like that

from bulletproof.

elieobeid7 avatar elieobeid7 commented on June 2, 2024

problem fixed

from bulletproof.

samayo avatar samayo commented on June 2, 2024

How did you fix it? I was just about to try it after work .. sorry

from bulletproof.

elieobeid7 avatar elieobeid7 commented on June 2, 2024

not really fixed, i thought it was but not really

https://stackoverflow.com/questions/46851647/cannot-insert-input-type-file-to-array-of-html-names

I don't know why SO votes down a question if it's hard to answer, anyhow I just discovered that you cannot add file input to html name array. unless you have a solution, I'd just add it to the array using php

from bulletproof.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.