0

I want to upload photo from file, it is working good if i try to save to

image/photo.jpg my file is in this folder named "index.php"

but when i want to save this file to 2 folder back like ../../img/testimonial/photo.jpg

it is giving this error Fatal error: Uncaught ValueError: Path cannot be empty in

here is my html code

<form action="code_register.php" method="post" enctype="multipart/form-data">
<label for="">Image</label>
<input type="file" id="uploadfile" name="uploadfile" class="form-control" accept="image/jpeg">
<button type="submit" id="add_ben" name="add_ben" class="ladda-button btn btn-primary" data-style="expand-right">Add Testimonial</button>
</form>

Here is my code_register.php

$fname="photo";
                                       $filename= $_FILES["uploadfile"]["name"];
                                       $tempname =$_FILES["uploadfile"]["tmp_name"];
                                       $realpath =$_FILES["uploadfile"]["full_path"];
                                       $filesize=$_FILES["uploadfile"]["size"];
                                       $folder = "../../img/testimonial/" . $fname . time() . ".jpg";
                                       $imginfo=getimagesize($tempname);
                                       $mime=$imginfo['mime'];
                                               switch($mime)
                                               {
                                                 case 'image/jpeg':
                                                       $image = imagecreatefromjpeg($tempname);
                                                       break;
                                                 case 'image/png':
                                                       $image = imagecreatefrompng($tempname);
                                                       break;
                                                 default:
                                                       $image = imagecreatefromjpeg($tempname);
                                               }


                                             if (isset($image))
                                             {
                                               $image = imagecreatefromjpeg($tempname);
                                               imagejpeg($image,$folder,40);
                                             }else {
                                               //echo "invalid file!";
                                             }

       $sql="INSERT INTO front_testimonial(image,createdon,created_by)
       VALUES ('$folder','$fcreateddate','$fcreated_by')";
        if( mysqli_query($mysqli,$sql)){
          $success="Testimonial has been added.";
}
  • 2
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Mar 04 '23 at 15:56
  • `$fname` is not defined in your code. `$fname` replace to `$filename` – iamafish Mar 04 '23 at 15:58
  • i have forgot to write it , it is defined on top with $fname = "photo"; – Saddam Hussain Chisti Mar 04 '23 at 16:06
  • Then `$folder = $_SERVER['DOCUMENT_ROOT'] . "/img/....` – iamafish Mar 04 '23 at 16:12
  • It is taking the root folder path, and path is saving in the sql. but still image is not saving in the specific folder. – Saddam Hussain Chisti Mar 05 '23 at 03:07

0 Answers0