Jump to content

Recommended Posts

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
:D

 

Hope you all help - Thanks

Game_Replays

Link to comment
https://forums.phpfreaks.com/topic/272237-question-website-form-data/
Share on other sites

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 by AoTBuNgLe
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.