0

Hi I'm trying to take make a picture upload feature.In this the user selects a picture file in a form.I'm trying to validate whether the file uploaded is a picture or not.If yes then the picture should be saved in a folder named picture in the same parent folder as the html and php file. I'm trying but the picture is not getting validated or saved.Please help..

MY HTML file upload.html

 <html>
 <body>

 <form action="upload_file.php" method="post"
  enctype="multipart/form-data">
 <label for="file">Filename:</label>
 <input type="file" name="file" id="file"><br>
 <input type="submit" name="submit" value="Submit">
 </form>

 </body>
 </html> 

MY PHP file upload.php

     <?php
     $allowedExts = array("gif", "jpeg", "jpg", "png");
     $temp = explode(".", $_FILES["file"]["name"]);
     $extension = end($temp);

     if ((($_FILES["file"]["type"] == "image/gif")
     || ($_FILES["file"]["type"] == "image/jpeg")
     || ($_FILES["file"]["type"] == "image/jpg")
     || ($_FILES["file"]["type"] == "image/pjpeg")
     || ($_FILES["file"]["type"] == "image/x-png")
     || ($_FILES["file"]["type"] == "image/png"))
     && ($_FILES["file"]["size"] < 20000)
     && in_array($extension, $allowedExts)) {
     if ($_FILES["file"]["error"] > 0) {
     echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
     } else {
     echo "Upload: " . $_FILES["file"]["name"] . "<br>";
     echo "Type: " . $_FILES["file"]["type"] . "<br>";
     echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
     echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
     if (file_exists("upload/" . $_FILES["file"]["name"])) {
     echo $_FILES["file"]["name"] . " already exists. ";
     } else {
     move_uploaded_file($_FILES["file"]["tmp_name"],
     "upload/" . $_FILES["file"]["name"]);
     echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
     }
     }
     } else {
     echo "Invalid file";
     }
     ?> 
4
  • check with your destination folder. you can try with '/upload/' May 2, 2014 at 6:45
  • Script is working. Please check your file names! action="upload_file.php", and your php file should be named accordingly: upload_file.php. Also, currently - your upload size limit is 20kb. And, you should have 'upload' folder inside test folder on your localhost (test folder - folder where your html and php files are in)
    – sinisake
    May 2, 2014 at 6:50
  • and also check folder having writing permission or not
    – Dexter
    May 2, 2014 at 6:51
  • p.s. if you want to change folder name for file uploads - make folder called 'picture' and change it in code - 'picture' instead 'upload'.
    – sinisake
    May 2, 2014 at 6:54

2 Answers 2

1

I think you may have to change your form action to upload.php Because You saved the php file with the name upload.php

0

Use this Code

if($_FILES['image']['name'])

    {
    $target='images/'.$_FILES['image']['name'];
    $source=$_FILES['image']['tmp_name'];
    copy($source,$target);
    }
1
  • images is folder name
    – Satyam
    May 2, 2014 at 6:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.