0

I am currently working on a site, of which needs to have it be possible for users to upload an image file. I have the HTML form setup and it seems to be working, this isn't the part with the issue. The issue is the PHP part, where I am getting the file upload, gathering the extension of that file and renaming the file to a specific timestamp and then uploading it into a specific folder.

I have an if statement to check for the upload, but every time I run the code it returns the "error" statement and when it "echo's" the file name it is missing the extension.

I have tried to run the code through syntax error checkers and nothing returns and when running the code it doesn't show any alerts.

I am not completely sure where the code goes wrong.

Below here is the HTML form

<form action="uploadProdu.php" method="POST" id="productUploadForm"> 

<input type="file" name="productImage" id="uploadImageFile" class="center-block"><br>  
<input type="submit" value="Submit Product" id="submitFormButton"> 

</form>

Below here is the PHP part

<?php 

     //This retrieves all the other information from the form 
     $productImage=($_FILES['productImage']['name']); 

     //Get the extension of the image uploaded
     $imageExtens = pathinfo($productImage, PATHINFO_EXTENSION);

    //Generate a timestamp to use and rename the file upload
     $ran = round(microtime(true)) ;

     //Add a dot to the random number, to then add extension later
     $ran2 = $ran.".";

     //Subdirectory for image upload (the targeted folder)
     $target = "productimages/";

    //Combine the folder, the timestamp name and the extension 
    $target = $target . $ran2 . $imageExtens;

    if(move_uploaded_file($_FILES['productImage']['tmp_name'], $target)) {
     echo "The file has been uploaded as ".$ran2.$imageExtens;
     }  else {
     echo "Sorry, there was a problem uploading your file.";
     echo "The file you tried to upload was called ".$ran2.$imageExtens;
      echo "And with directory full length ".$target;
     }
     ?> 
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
DevLiv
  • 573
  • 7
  • 19
  • 1
    Your form is missing the enctype attribute with the value necessary for HTTP file uploads to work ... – CBroe Jan 05 '18 at 00:04
  • @CBroe oh well, so it is the form upload I guess then. I appreciate you pointing it out, I will look into it. Thank you. – DevLiv Jan 05 '18 at 00:05
  • 2
    @CBroe just added the attribute as you said and it is now working, thank you very much. If you want to you can add it as an answer, so I am able to accept it. – DevLiv Jan 05 '18 at 00:11
  • This has been asked many a time; the duplicate for this contains it. The manual on uploading files also contains it. https://secure.php.net/manual/en/features.file-upload.post-method.php – Funk Forty Niner Jan 05 '18 at 00:17
  • 1
    @FunkFortyNiner I am aware of the issue now as CBroe pointed it out, however when posting the question I had no idea as the PHP guide I tried to check, didn't include the HTML part. Also I wasn't even getting the "Undefined variable" error, which makes it a little difficult to know what to search for. Nevertheless, thank you for the "heads up". – DevLiv Jan 05 '18 at 00:22

0 Answers0