Results 1 to 9 of 9

Thread: PHP File Upload Script

  1. #1
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Default PHP File Upload Script

    Hey I need a piece of PHP code that will help my user's upload their files. I need to have it in a function or something that i can do this:
    PHP Code:
    if($secret==somenumber){
    uploadthatfilenow();
    }{
    echo 
    "Please fill refill out the form...";

    I do not need a file upload form... I just need a piece of code that can upload the file, check to make sure it is either a jpg, gif, mov, flv, png, jpeg...
    I also need for the file to be renamed so that its the next number...
    Example below...
    Code:
    A user uploads a file named "funny.jpg" it gets renamed to "1.jpg"
    Another user later uploads a file named "JOKE.mov" 1.mov
    Another user uploads a file named "FUNNY.jpg" it gets renamed to "2.jpg"
    Basically I think the above is a system of make the filename into 1 and then checking to see if that file name 1 is taken and then increase the number...

    I also need the file to be saved into a folder called "Pending"...

    This is one area I can't quite get... hopefully if someone can really explain how todo this or better yet just give me the code and so that I can look at it and make sense. Thanks to those who help

  2. #2
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    building the upload script is really kinda heavily dependant on the data being sent to it... you are asking for a custom script, but telling us to create a generic script.

    we helped you build a script before and there are multiple other threads here on upload scripts. If you change your script from before into a function you could very easily modify it to your specifications.

    now on the folder / file name concept. I would suggest making a folder called pending, and then sub folders for each format that you wish to support. this will help separate between the different file types that you are allowing.

  3. #3
    Join Date
    Oct 2005
    Posts
    255
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    here you go, try this out..

    Code:
    <?php
    
      error_reporting(E_ALL ^ E_NOTICE); // Show all major errors.
    
      // Check to see if the button has been pressed
      if (!empty($_REQUEST['sendForm']))
      {
        // Assign the name to a variable
        $name = $_FILES['uploaded_file']['name'];
        // Assign the tmp_name to a variable
        $tmp_name = $_FILES['uploaded_file']['tmp_name'];
        // Assign the error to a variable
        $error = $_FILES['uploaded_file']['error'];
        // Assign the size to a variable
        $size = $_FILES['uploaded_file']['size'];
        // No trailing slash
        $uploadFilesTo = 'imgs';
        // Create safe filename
        $name = ereg_replace('[^A-Za-z0-9.]', '-', $name);
        // Disallowed file extensions
        $naughtyFileExtension = array("php2", "php3", "php4", "php5", "asp", "inc", "txt", "wma", "mov", "js", "exe", "jsp", "map", "obj", " ", "", "html", "mp3", "mpu", "wav", "cur", "ani", "fla", "swf");
        // Returns an array that includes the extension
        $fileInfo = pathinfo($name);
        // Check extension
        if (!in_array($fileInfo['extension'], $naughtyFileExtension))
        {
          // Get filename
          $name = getNonExistingFilename($uploadFilesTo, $name);
          // Upload the file
          if (move_uploaded_file($tmp_name, $uploadFilesTo.'/'.$name))
          {
              // Show success message
              echo '<center><a href="http://www.xudas.com/forum/imgupload/'.$uploadFilesTo.'/'.$name.'" target="_blank">
    <img src="http://www.xudas.com/forum/imgupload/'.$uploadFilesTo.'/'.$name.'" align="top" width="100" border="0"></a>
    <textarea>http://www.xudas.com/forum/imgupload/'.$uploadFilesTo.'/'.$name.'</textarea></center>';
          }
          else
          {
              // Show failure message
              echo '<center><SPAN style="color:gray">File failed to upload to /'.$name.'</span></center>';
          }
        }
        else
        {
            // Bad File type
            echo '<center><SPAN style="color:gray">The file uses an extension we don\'t allow.</span></center>';
        }
      }
     
      // Functions do not need to be inline with the rest of the code
      function getNonExistingFilename($uploadFilesTo, $name)
      {
          if (!file_exists($uploadFilesTo . '/' . $name))
              return $name;
         
          return getNonExistingFilename($uploadFilesTo, rand(100, 200) . '_' . $name);
      }
    ?>
    <BR>
    <center>
    <table bgcolor="#000000" border="0" width="100%">
    <tr><td>
    <form action="" method="post"    enctype="multipart/form-data">
      <input type="file" name="uploaded_file">
      <input type="submit" name="sendForm" value="Upload">
    </form>
    
    </td></tr>
    </table>
    </center>
    Hey new design new look, goto xudas for personal webdsign help.. (:

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    A blank grey page?

    insanemonkey, you're still using that bad upload script?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Dec 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi, I'm using this code for file upload....
    its fine with images but how can i upload other formats such as Microsoft Word or Exel file or any simple text file

    Can anyone help me ??
    <?php
    if (($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/png")
    || ($_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";
    }
    ?>

  6. #6
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,625
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Check the following list
    Code:
    "application/pdf" - PDF Files
    "application/msword" - MS Word Files
    "application/powerpoint" - MS Powerpoint Files
    "application/excel" - MS Excel Files
    "text/plain" - Text Files
    Hope this helps

  7. #7
    Join Date
    Dec 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Thanks For The Help......

    Thanks alot !!!!
    It worked very well ....

    Haven't tried all formats but simple text is working and has solved my problem.....
    Thanks again...

  8. #8
    Join Date
    Dec 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Need Help Again..........

    Hi,
    i have 2 servers.
    I foubd that my server 2 doesn't supports Php so is there any other way
    like asp solution for it ??
    (recomending asp b'coz its supported by my server, i have only problem with .net and php anything else will do )

    Shriniwas

  9. #9
    Join Date
    Jun 2017
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Try out this php file upload script here: https://www.cloudways.com/blog/the-b...upload-in-php/ I was able to implement file upload feature in my chat application using this script. Only thing you would need to tweak is its support for multiple file upload.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •