Page 2 of 2 FirstFirst 12
Results 11 to 20 of 20

Thread: Upload using PHP

  1. #11
    Join Date
    Dec 2012
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    It just needs to be a one stop drop area. I do not want any other access to the backend (so no access to view, edit or delete already uploaded files).

    And I would like to keep it as a separate page to the contact area as this could create a feeling of special treatment to our existing and regular customers i.e. a section of our website purely for their use etc.

    Ideally as it will only be used occasionally (we tend to do most design and artwork ourselves), I think that maybe a simple PHP script would be suitable. And I have tried using the tiztag.com link as suggested by Bernie a couple weeks ago. After playing with it for a while, eventually I had it working!! But now, no matter what I do, I can not get it going again.

    Obviously I am trying to learn as much as possible as quickly as possible as my directors have scheduled a deadline of the first week in January for me to have this finished and ready to present! My primary role is a Production Manager. But due to tough times, the company made a decision that we should try to keep the new website build in house to save costs and decided I would be the best person to take this task on. I am fairly tech savvy but Website building and programming was something I have never touched on before. And depending how things go this could be a service we offer later in the future (obviously after much more training and learning). But anyway... enough of the about me!!

  2. #12
    Join Date
    Dec 2012
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Ok so I have got the upload section working and correctly uploading and moving documents to the correct folder.

    I have set the limit to 100mb and included valid file extensions. I am having issues with uploading fonts. Have I inserted the correct mime type etc.? and are they correct for the other extensions?

    Also what I would like to do, is:

    - When the file upload is complete (or fails), I would like to direct to a new pre-designed web page (that will fit within the design rather than have some small text on a white page). How would I put this link in to the php script? My php script at the moment is:

    PHP Code:
    <?php
    $allowedExts 
    = array("jpg""jpeg""gif""png""pdf""cdr""ai""eps""indd""tif""tiff""bmp""ttf""otf",);
    $extension end(explode("."$_FILES["uploadedfile"]["name"]));
    if (((
    $_FILES["uploadedfile"]["type"] == "image/gif")
    || (
    $_FILES["uploadedfile"]["type"] == "image/jpeg")
    || (
    $_FILES["uploadedfile"]["type"] == "image/png")
    || (
    $_FILES["uploadedfile"]["type"] == "application/pdf")
    || (
    $_FILES["uploadedfile"]["type"] == "application/cdr")
    || (
    $_FILES["uploadedfile"]["type"] == "application/ai")
    || (
    $_FILES["uploadedfile"]["type"] == "application/eps")
    || (
    $_FILES["uploadedfile"]["type"] == "application/indd")
    || (
    $_FILES["uploadedfile"]["type"] == "image/tif")
    || (
    $_FILES["uploadedfile"]["type"] == "image/tiff")
    || (
    $_FILES["uploadedfile"]["type"] == "image/bmp")
    || (
    $_FILES["uploadedfile"]["type"] == "font/ttf")
    || (
    $_FILES["uploadedfile"]["type"] == "font/otf")
    || (
    $_FILES["uploadedfile"]["type"] == "image/pjpeg"))
    && (
    $_FILES["uploadedfile"]["size"] < 104900000)
    && 
    in_array($extension$allowedExts))
      {
      if (
    $_FILES["uploadedfile"]["error"] > 0)
        {
        echo 
    "Return Code: " $_FILES["uploadedfile"]["error"] . "<br>";
        }
      else
        {
        echo 
    "Thank you. Your file was uploaded sucessfully." ."<br>";
      

        if (
    file_exists("uploads/" $_FILES["uploadedfile"]["name"]))
          {
          echo 
    $_FILES["uploadedfile"]["name"] . " already exists. ";
          }
        else
          {
          
    move_uploaded_file($_FILES["uploadedfile"]["tmp_name"],
          
    "uploads/" $_FILES["uploadedfile"]["name"]);
          echo 
    "";
          }
        }
      }
    else
      {
      echo 
    "Invalid file type. Please try again or contact us for advice.";
      }
    ?>
    Last edited by traq; 12-11-2012 at 03:54 PM. Reason: please use the forum's [php][/php] BBCode tags when posting php code

  3. #13
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    The file extension is not a good way to determine the file type. You'd be better off checking the file's mime type (the fileinfo functions are good for this):
    PHP Code:
    <?php

    $file 
    /* path to file you're checking */;
    $allowedMimeTypes = array( 'image/gif','image/jpeg','etc.' );

    $finfo finfo_open(FILEINFO_MIME_TYPE);
    if( 
    in_arrayfinfo_file($finfo$filename), $allowedMimeTypes ) ){ /* okay then */ }
    else{ 
    /* not an allowed file type */ }
    What version of PHP are you running? [icode]finfo[icode] is included by default from 5.3 on. If there's any trouble, you may need to check your server configuration (finfo uses the host computer's "magic" mime hinting files).

    Also be aware that for large file uploads, you may need to change the max upload filesize (both in your php.ini and in Apache). Talk to your web host about this.
    Last edited by traq; 12-12-2012 at 02:51 AM.

  4. The Following User Says Thank You to traq For This Useful Post:

    spiper (12-13-2012)

  5. #14
    Join Date
    Dec 2012
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    After playing with it, I see there is an obvious error in the script as well.
    If I try and upload an image with the same name as an existing. I will get the message "Thank you. Your file was uploaded successfully. 'your file name' already exists"
    So obviously the script is reporting success before it moves the file. I have tried moving snippets of script around but I am struggling. I think I am missing something!!

  6. #15
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    I'm out right now but I'll have a look this afternoon.

  7. #16
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    46
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    Try this:
    PHP Code:
     if ($_FILES["uploadedfile"]["error"] > 0) {
        echo 
    "Return Code: " $_FILES["uploadedfile"]["error"] . "<br>";
        }
    elseif (!
    file_exists("uploads/" $_FILES["uploadedfile"]["name"])) {
        echo 
    "Thank you. Your file was uploaded sucessfully." ."<br>";
        }
    elseif (
    file_exists("uploads/" $_FILES["uploadedfile"]["name"])) {
        echo 
    $_FILES["uploadedfile"]["name"] . " already exists. ";
        }
    else {
        
    move_uploaded_file($_FILES["uploadedfile"]["tmp_name"],
        
    "uploads/" $_FILES["uploadedfile"]["name"]);
        echo 
    "";
        } 
    The issue is that there is no error, so it passes through to the else of the if statement and thus returns that it uploaded correctly. It then goes through to the other if statement, and because there are two, it also echo's that there is already a file with the same name there. This code should fix that.
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

  8. #17
    Join Date
    Dec 2012
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default


    I used this code. I get the thank you message but file does not appear to have been moved to the uploads/ dir. Also I am a little confused at the code:

    If under 100mb
    and file extension allowed
    if more than 0 then show "error."
    Else if file exists then show "thank you"
    Else if file exists the show "already exists"
    Else move file to uploads/.

    Obviously I am an amateur so not questioning your expertise, I just can not see the logic in the code. I thought it would be more along the lines:

    If under 100mb (Else show "Size too big")
    and file extension allowed (Else show "Invalid File Type")
    and file does not exist in uploads/ (Else show "already exists")
    Then move file to uploads/ and show "Thank you" (Else show "Error with upload")

    This is what I want to say but just dont know how!

  9. #18
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    46
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    I've put it in a bit of a different order, but I see where I went wrong:
    PHP Code:
     if ($_FILES["uploadedfile"]["error"] < && !file_exists("uploads/" $_FILES["uploadedfile"]["name"])) {
        echo 
    "Thank you. Your file was uploaded sucessfully." ."<br>";
        
    move_uploaded_file($_FILES["uploadedfile"]["tmp_name"],
        
    "uploads/" $_FILES["uploadedfile"]["name"]);
        echo 
    "";
        }
    elseif (
    file_exists("uploads/" $_FILES["uploadedfile"]["name"])) {
        echo 
    $_FILES["uploadedfile"]["name"] . " already exists. ";
        }
    else {
        echo 
    "Return Code: " $_FILES["uploadedfile"]["error"] . "<br>";
        } 
    NB: I didn't integrate traq's file checking as it doesn't have all the file types you specified, and it should be fairly easy for you to do it.
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

  10. #19
    Join Date
    Dec 2012
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Thank you for your response but I still could not get the above code working. But... after playing around and a lot of trial and area, I got there in the end. Here is the code that eventually worked:

    PHP Code:
     {
      if (
    $_FILES["uploadedfile"]["error"] > 0)
        {
        echo 
    "Return Code: " $_FILES["uploadedfile"]["error"] . "<br>";
        }
        {
    if (
    file_exists("uploads/" $_FILES["uploadedfile"]["name"]))
          {
          echo 
    $_FILES["uploadedfile"]["name"] . " already exists. ";
          }
        else
          {
          
    move_uploaded_file($_FILES["uploadedfile"]["tmp_name"],
          
    "uploads/" $_FILES["uploadedfile"]["name"]);
          echo 
    "Thank you. Your file was uploaded sucessfully.";
          }
        }
      }
    else
      {
      echo 
    "Invalid file type. Please try again or contact us for advice.";
      } 
    Just one other thing... Instead of displaying a simple message, I want to redirect to another web page. I imagine this snippet of script would be quite simple but I have never used it before so an example would be much appreciated.
    Last edited by keyboard; 12-13-2012 at 09:50 PM. Reason: Format: Php Tags [php][/php]

  11. #20
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    2,038
    Thanks
    16
    Thanked 316 Times in 314 Posts
    Blog Entries
    13

    Default

    Code:
    header("Location: http://website.com/path/to/thanks.htm");
    instead of;
    Code:
    echo "Thank you. Your file was uploaded sucessfully.";
    should hopefully do the redirect.
    Focus on Function Web Design | Latest News RSS | Facebook | Twitter |
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps |
    The only limit to creativity is imagination: JemCon.org

  12. The Following User Says Thank You to Beverleyh For This Useful Post:

    spiper (12-13-2012)

Similar Threads

  1. how can i do file upload control to upload any image
    By Rai_87 in forum MySQL and other databases
    Replies: 0
    Last Post: 07-04-2008, 02:36 AM
  2. Replies: 2
    Last Post: 03-15-2007, 10:11 AM
  3. Upload
    By egrave in forum JavaScript
    Replies: 3
    Last Post: 10-21-2006, 03:39 PM
  4. how can i upload
    By king2tc in forum PHP
    Replies: 0
    Last Post: 08-15-2006, 05:21 PM
  5. help need in upload
    By pkcidstudio in forum MySQL and other databases
    Replies: 1
    Last Post: 06-14-2006, 05:27 PM

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
  •