0

This is the upload form

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

And this is the php:

<?php

    $keys = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    for ($i = 0; $i < 10; $i++) { 
        $photoID .= $keys[rand(0, strlen($keys)-1)];
    } 

    //add a dot (.) to the randomly generated string so the ext can be applied to it later
    $photoID2 = $photoID.".jpg";

    //This assigns the subdirectory you want to save into... make sure it exists!
    $target = "uploads/";

    //This combines the directory, the random file name, and the extension
    $target = $target . $photoID2.$ext; 

    if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) {
        echo "The file has been uploaded as ".$photoID2.$ext;
    } else {
        echo "Error: upload did not work";
    }
?>

The problem I have is that i keep getting the error upload did not work... what am I doing wrong here ? it's really basic something that I am missing but i need to understand it because I can already do file uploads fine but want to understand how it works...

2
  • Make sure the target folder in $target exists and $target has suffient permissions (chmod) Commented Dec 2, 2013 at 10:12
  • Well, you have to look what the error actually is to fix it. How to do that? PHP writes down its error. Take a look into the log file (http servers error log) and you have your answer. Commented Dec 2, 2013 at 10:14

4 Answers 4

1

First:
make sure that there is a directory with the name uploads
second:
Try to give the suitable permissions to the directory using chmod like

chmod -R 777 /path/to/the/directory
Sign up to request clarification or add additional context in comments.

Comments

0

Use below code for renaming a uploaded file

$name = $_FILES['Fixtures']['uploaded']['name'];
$tmp_name = $_FILES['uploaded']['tmp_name'];
$target_path = "images/uploads/";
$extension = end(explode('.', $name));
$randomName = 'thumbnail_' . rand(123456, 1234567890) . '.' . $extension;
/* Add the original filename to our target path.  
  Result is "images/uploads/filename.extension" */
$target_path = $target_path . basename($randomName);
$allowedImageTypes = array("image/jpeg", "image/jpg", "image/png", "image/x-png", "image/gif");
if (in_array($type, $allowedImageTypes)) {
    move_uploaded_file($tmp_name, $target_path) or die("error in thumbnail upload!");
}

This will upload all the files with renaming the files

2 Comments

what is the ['Fixtures'] ? ... will give this a try and see if it works.
Sorry that fixtures in my model name you can use your model name in which your form is working
0

Check the folder permissions of uploads/ if it's writeable for apache.

And try to set the error reporting so move_uploaded_file can tell you what's wrong.

Comments

0

I think I will settle with this solution, it makes more sense to me, the move_uploaded_files is the one that puts it in the directory and gives it the name.

<?php 

  //create random file name
    $keys = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

     for ($i = 0; $i < 7; $i++)    
        {                         
      $key .=  $keys[rand(0, strlen($keys)-1)];      
      }

 //get the extension
  $ext = basename( $_FILES['uploaded']['type']);

 //choose destination, add filename and extension
   $target = "uploads/" .$key. "." . $ext; 


 //move the file to the des
   if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
 {
  echo "The file has been uploaded";
  } 
  else {
 echo "Sorry, there was a problem uploading your file.";
 }
  ?> 

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.