Game_Replays Posted December 20, 2012 Share Posted December 20, 2012 Hi there, I am working on a website and I have this code to allow people to upload to the website: Form: <form action="../scripts/upload_file.php" method="post" enctype="multipart/form-data"> <label for="file">Email Address:</label> <input name="email" type="text" id="email"><br> <label for="file">Username:</label> <input name="name" type="text" id="name" maxlength="12"><br> <label for="file">Select File:</label> <input type="file" name="file" id="file"><br> <input type="submit" name="submit" value="Submit"> </form> PHP script: <?php $allowedExts = array("SC2Replay"); $extension = end(explode(".", $_FILES["file"]["name"])); if ((($_FILES["file"]["type"] == "file/.SC2Replay")) && isset($_get["emai"]["name"]) && in_array($extension, $allowedExts)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; } else { echo "Email: " . $_GET["email"]["name"] . "<br>"; echo "Uploader: " . $_GET["name"]["name"] . "<br>"; 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 I want to be able to do: The thing I want to do with the form and PHP Script is for the form to ask for: Email Name File Once it has those three things it will then allow you to upload to the website (I know the file format is only ".SC2Replay") Once it is uploaded how will it look on a FTP server? "NameOfFile.SC2Replay" or "Username_NameOfFile.SC2Replay" ? If it looks like the first one then i'd like to have a snippet of code to make it turn into "Username_NameOfFile.SC2Replay" if that is possible Hope you all help - Thanks Game_Replays Quote Link to comment Share on other sites More sharing options...
Game_Replays Posted December 21, 2012 Author Share Posted December 21, 2012 ;_; No replies really? Quote Link to comment Share on other sites More sharing options...
Game_Replays Posted December 24, 2012 Author Share Posted December 24, 2012 ...? Quote Link to comment Share on other sites More sharing options...
Jessica Posted December 24, 2012 Share Posted December 24, 2012 You didn't ask a question. Quote Link to comment Share on other sites More sharing options...
Game_Replays Posted December 26, 2012 Author Share Posted December 26, 2012 Once it is uploaded how will it look on a FTP server? "NameOfFile.SC2Replay" or "Username_NameOfFile.SC2Replay" ? Quote Link to comment Share on other sites More sharing options...
Jessica Posted December 26, 2012 Share Posted December 26, 2012 Well since there's nothing in the code to append the username.... Quote Link to comment Share on other sites More sharing options...
Love2c0de Posted December 26, 2012 Share Posted December 26, 2012 (edited) As your code stands it would look like: NameOfFile.SC2Replay If you want to add the username to the beginning of the file string, you need to concatenate it before moving the file if my memory serves me correctly. Also, instead of sending them an error message saying the file already exists, what I have done previously is generate a random number between 10 and 99 then concatenate the result to the end of the file name, before the extension but making sure you let the user know you had to edit the filename. Not sure if you should edit the filename or send an error but I'd much prefer my site to edit the filename and send a success message than to send an error message because if the user really wants to upload it, he will just go and edit the filename and attempt to re-upload it. He could potentially rename it to the same name as another file in your DB and have the same error which would potentially cause the user to not bother and leave the site. By using a while loop and checking the return values of an expression, you can guarantee you will generate another string which is not in use already. For example: //lets say you have already got the file name and removed the extension. and saved it into $filename //$filename = "your_video_name" while (file_exists($filename)){ $filename.rand(10,99).$extension; } Code not tested but I believe you should understand the jist of it. Kind Regards, AoTB. Edited December 26, 2012 by AoTBuNgLe Quote Link to comment Share on other sites More sharing options...
Game_Replays Posted December 31, 2012 Author Share Posted December 31, 2012 Well since there's nothing in the code to append the username.... "uploader" 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.