Conor Posted March 7, 2010 Report Share Posted March 7, 2010 if i wanted to add an upload button to my website how would i go about it? basiclly i want users to be able to choose a file and upload it to http:// www.example.com/uploads/ Quote Link to comment Share on other sites More sharing options...
Moderators mark1million Posted March 7, 2010 Moderators Report Share Posted March 7, 2010 Just a quick demo from w3 schools, <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> This will explain everything, http://www.w3schools.com/PHP/php_file_upload.asp Quote Link to comment Share on other sites More sharing options...
Conor Posted March 7, 2010 Author Report Share Posted March 7, 2010 Parse error: syntax error, unexpected T_STRING in /home/a5694701/public_html/upload_file.php on line 39 Quote Link to comment Share on other sites More sharing options...
Moderators mark1million Posted March 7, 2010 Moderators Report Share Posted March 7, 2010 Whats on your line 39 of upload_file.php? Quote Link to comment Share on other sites More sharing options...
Conor Posted March 7, 2010 Author Report Share Posted March 7, 2010 && ($_FILES["file"]["size"] < 20000MB)) Quote Link to comment Share on other sites More sharing options...
Moderators mark1million Posted March 7, 2010 Moderators Report Share Posted March 7, 2010 All you should need is the form in the post above and the script below, <?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"; } ?> What that is doing is going to do is allow the upload a, "image/gif" "image/jpeg" "image/pjpeg" To a folder on your server called "upload/" change to what you want and you will need to allow write permissions Quote Link to comment Share on other sites More sharing options...
Moderators mark1million Posted March 7, 2010 Moderators Report Share Posted March 7, 2010 Remove the MB Quote Link to comment Share on other sites More sharing options...
Conor Posted March 7, 2010 Author Report Share Posted March 7, 2010 Ok so this is my upload_file.php <?phpif ($_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: " . $_FILES["file"]["tmp_name"]; } ?> <?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: " . $_FILES["file"]["tmp_name"]; } ?> <?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 "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: " . $_FILES["file"]["tmp_name"]; } } else { echo "Invalid file"; } ?> <?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("/wp-content/uploads/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "/wp-content/uploads/" . $_FILES["file"]["name"]; } } } else { echo "Invalid file"; } ?> Quote Link to comment Share on other sites More sharing options...
Moderators mark1million Posted March 7, 2010 Moderators Report Share Posted March 7, 2010 I think i see what you have done, clear that file and use, <?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("/wp-content/uploads/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "/wp-content/uploads/" . $_FILES["file"]["name"]; } } } else { echo "Invalid file"; } ?> Try that see how you get on. Quote Link to comment Share on other sites More sharing options...
Conor Posted March 7, 2010 Author Report Share Posted March 7, 2010 i keep getting InvalidFile Quote Link to comment Share on other sites More sharing options...
Tom Posted March 7, 2010 Report Share Posted March 7, 2010 Is the file you're uploading a jpg, pjpg (whatever that is) or a gif and under 20mb? Quote Link to comment Share on other sites More sharing options...
Conor Posted March 7, 2010 Author Report Share Posted March 7, 2010 Is the file you're uploading a jpg, pjpg (whatever that is) or a gif and under 20mb? i tried a .jpg and .gif and i wouldnt think any picture could be over 20MB these ones definetly arent over that they where very small banners you can try yourself if you want http://fshanger.webatu.com/upload.php Invalid File, upload a .zip file only dont mind the last bit the file extenions are still jpg, jpeg, gif i just changed that bit because i plan to only allow .zip files Quote Link to comment Share on other sites More sharing options...
Moderators mark1million Posted March 7, 2010 Moderators Report Share Posted March 7, 2010 If its complaining about the file type then remove the checking from the loop and add one at a time back in to it see whats happening, sorry i dont have much time at the mo or i would test for you. Quote Link to comment Share on other sites More sharing options...
Moderators mark1million Posted March 7, 2010 Moderators Report Share Posted March 7, 2010 OK this is really basic and i mean basic. Your html form, <form enctype="multipart/form-data" action="upload_file.php" method="POST"> Please choose a file: <input name="uploaded" type="file" /><br /> <input type="submit" value="Upload" /> </form> Your upload script Change the target to your folder you want to upload to <<?php $target = "test/"; $target = $target . basename( $_FILES['uploaded']['name']) ; $ok=1; if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else { echo "Sorry, there was a problem uploading your file."; } ?> Just tested the above example and it works OK on my test server Quote Link to comment Share on other sites More sharing options...
Conor Posted March 7, 2010 Author Report Share Posted March 7, 2010 I'll check that out tomorow after school I'll let you knw how It turns out Quote Link to comment Share on other sites More sharing options...
Conor Posted March 9, 2010 Author Report Share Posted March 9, 2010 Connection times out Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.