Hi all, Just need a very basic jpg uploader - put this together and it is giving no errors but I can't find the picture in the folder? I can't find it anywhere on my file manager? <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> PHP: upload_file.php <html> <body> <?php if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_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 "Stored in: " . "upload/" . $_FILES["file"]["name"]; } ?> </html> PHP: I get: Any ideas? Thank you.
Check out the notes on http://www.php.net/manual/en/features.file-upload.php You have to move the uploaded file, it will still be in a temp folder you can't/shouldn't access directly.
Hi Sarahk, thanks for your reply. I tried the below code and I got permission denied? I created a folder called "upload" but no pix is in it :/ <?php if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 20000)) { 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"; } ?> PHP:
"upload/" probably isn't the full path to the directory have you set the directory permissions to 777
Excellent, sarahk, I put the folder 777 and it works now! Excellent! Thanks very much. If I wanted to alter the above text to just overwrite the file everytime - could you edit it for that please?
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); PHP: becomes move_uploaded_file($_FILES["file"]["tmp_name"], "upload/permanent_name.txt"); PHP: replace permanent_name.txt with whatever the file name should be.
Hi Sarahk, I've tried the code in a page on my site and it won't work - any suggestions please? Link is here: http://standoutmodels.com/upload.php
Just focussing on the php section <?php if (isset($_FILES)) { $valid_types = array("image/gif", "image/jpeg", "image/pjpeg"); if (in_array($_FILES["file"]["type"], $valid_types) && ($_FILES["file"]["size"] < 20000)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br /> Type: " . $_FILES["file"]["type"] . "<br /> Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br /> Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; $new_file = 'upload/' . $_FILES['file']['name']; if (file_exists($new_file)) { echo $new_file . ' already exists. '; } else { move_uploaded_file($_FILES["file"]["tmp_name"], $new_file); echo "Stored in: " . $new_file; } } } else { echo "Invalid file"; } } else echo "No file selected for upload"; ?> PHP: but all I've really done is improve the read-ability and put a check to see if a file had been uploaded in the first place. I have no idea where your upload form is and there are no obvious reasons to be getting errors. What isn't actually working for you?
Hello Sarahk, thanks for the reply. I've created a folder called "upload" and the file itself is called standout.jpg Its located at http://standoutmodels.com/upload/standout.jpg The folder is set to 777 and the upload.php is also set to 777. Here is the link for the upload file: You will see yourself, I only get "Invalid file" - no option to overwrite standout.jpg http://www.standoutmodels.com/upload.php
Where is your form? The link itself takes me to the page but unless I can select an image on my computer and submit that form I can't really test.
Just the same as my very first post. I tried a different file size and it worked there. http://www.standoutmodels.com/upload_file.php It won't overwrite the original file but I didn't implement those changes. Thanks very much Sarahk