I'm brand new to php and am looking to understand file uploading. I pulled a basic script, but when I try to upload a photo I get an error message at the end.

Here is the code I am using. The files should be uploaded to a file named "uploads"
At the end I just get the No File uploaded error message, but since I'm new at php and don't have a good grasp of each line I'm not even sure what is wrong or what to fix. Any help would be greatly appreciated.

//Сheck that we have a file
if((!empty($FILES["uploaded_file"])) && ($FILES['uploaded_file']['error'] == 0)) {
//Check if the file is JPEG image and it's size is less than 350Kb
$filename = basename($FILES['uploaded_file']['name']);
$ext = substr($filename, strrpos($filename, '.') + 1);
if (($ext == "jpg") && ($
FILES["uploaded_file"]["type"] == "image/jpeg") &&
($FILES["uploaded_file"]["size"] < 350000)) {
//Determine the path to which we want to save this file
$newname = dirname(FILE).'/uploads/'.$filename;
//Check if the file with the same name is already exists on the server
if (!file_exists($newname)) {
//Attempt to move the uploaded file to it's new place
if ((move_uploaded_file($
FILES['uploaded_file']['tmp_name'],$newname))) {
echo "It's done! The file has been saved as: ".$newname;
} else {
echo "Error: A problem occurred during file upload!";
}
} else {
echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";
}
} else {
echo "Error: Only .jpg images under 350Kb are accepted for upload";
}
} else {
echo "Error: No file uploaded";
}

    did you change the permission of /uploads/ to allowing writing?

      I'm not even sure how to do that. Since I'm so new at php that is probably the issue, where can I find a tutorial on how to do that?

        I figured out how to make the file writable...and it now is. I'm still getting the error message.

          Parse error, file upload error? If it's the latter, how about

          else {
          // Replace this: echo "Error: No file uploaded";
          echo "Error uploading file: " . $_FILES['uploaded_file']['error'];
          }

          And I'm just taking a wild stab as to where that piece of code really should go. You should use [ php ] and [ /php ] tags (without spaces) and indent your code properly.

            Write a Reply...