How to upload a predefined filename on a server with PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MOzgaf

    How to upload a predefined filename on a server with PHP

    hi
    am novice at php but I hope that ye will help me.I have a long list of names of text files.these txt files will be used by swf to displayed on the flash file.the problem is to make a user friendly upload interface that make it easy for people to upload the correct or incorrect txt file name and choose to overwrite a certain text file.example:
    this is html form:
    Upload a file: /file_location/ |Browse button|
    Choose file name |fileName1.txt| or|fileName2.tx t|or....
    |submit button|

    but I cant send the file name option to php to overwrite the existing fileName.
    If this is still difficult to understand please tell me.
    Thank you.
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    How are you allowing users to specify the 'correct filename'? an input element?

    Comment

    • MOzgaf

      #3
      ill show you better :
      html
      Code:
      <form enctype="multipart/form-data" action="upload.php" method="post">
          <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
          Choose a file to upload: </br>
          <input name="uploaded_file" type="file" /></br>
      
      <select name="nameFile" type="name">
      <option value="\mozgaf1.txt">mozgaf</option>
      <option value="\mozgaf2.txt">mozgaf2</option>
      <option value="\mozgaf3.txt">mozgaf3</option>
      </select>
      
      
      <input type="submit" name="submit" value="Submit" />
        </form>
      PhP file looks kind of like this.
      Code:
      <?php
      //check that we have a file
      if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
        //Check if the file is TEXT and it's size is less than 350Kb
        $filename = basename($_FILES['nameFile']['name']);
        $ext = substr($filename, strrpos($filename, '.') + 1);
        if (($ext == "txt") && ($_FILES["nameFile"]["type"] == "text/txt") && 
          ($_FILES["uploaded_file"]["size"] < 350000)) {
          //Determine the path to which we want to save this file
            $newname = dirname(__FILE__).'/upload/'.$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["nameFile"]["name"]." already exists";
            }
        } else {
           echo "Error: Only .txt files under 350Kb are accepted for upload";
        }
      } else {
       echo "Error: No file uploaded";
      }
      ?>
      This way it changes the file name that i pick.

      Comment

      Working...