SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast stonelord's Avatar
    Join Date
    Nov 2008
    Location
    Olney, MD - USA
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Problem with uploading file code - Help please!

    Hi, Below is my php code for uploading a file through a website. It seems to be working ok, but when you try to upload a .tiff file it will not work and produce any type of error. I know that by letting people upload files you are opening up a can of worms, but my customers need to be able to upload their design files to me. Any suggestions on improving this code? is it safe? I also have it setup so that after someone uploads a file, it sends me an email, but it goes to my junk mail for some reason, any ideas?

    Thanks for the feedback.

    Code:
    <?php
    
    	$name = $_POST['name'];
    	$company = $_POST['company'];
    	$phone = $_POST['phone'];
        $message = $_POST['message'];
    	$email = $_POST['email'];	
        $allowed_filetypes = array('.jpg','.gif','.bmp','.png', '.eps', '.tiff', '.gif', '.psd', '.tif', '.jpeg');
        $max_filesize = 100000000;
        $upload_path = './files/';
     
       $filename = $_FILES['userfile']['name'];
       $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
     
       if(!in_array($ext,$allowed_filetypes))
          die('<h1 align="center" class="welcome">The file you attempted to upload is not allowed.</h1>');
     
       if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
          die('<h1 align="center" class="welcome">The file you attempted to upload is too large.</h1>');
    
       if(!is_writable($upload_path))
          die('<h1 align="center" class="welcome">You cannot upload to the specified directory, please CHMOD it to 777.</h1>');
    
       if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
             echo '<h1 align="center" class="welcome">Your file upload was successfully! We will be in touch with you shortly</h1>';
    		 echo '<br/>';
    		 echo '<h1 align="center" class="note">Note (<i>only if applicable</i>): Extra charge if file is not print ready</h1>'; 
    		 echo '<h1 align="center" class="note">Note (<i>only if applicable</i>): $10 re-rip fee</h1>'; 
    		 mail("test@gmail.com, test2@live.com","Image/File - Uploaded","
    			A file has been uploaded on your site
    			Name : $name
    			Company : $company
    			Email: $email
    			Phone : $phone
    			Message : $message
    			","From: $email");	 
    ?>
    Rodrigo Olivares
    Small Business Owner (Web & Graphic Design)
    www.fearmydesign.com

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A filename with an upper-case file extension will be rejected, so are you uploading a .TIFF file rather than a .tiff file?
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  3. #3
    SitePoint Enthusiast stonelord's Avatar
    Join Date
    Nov 2008
    Location
    Olney, MD - USA
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation

    Mark, thanks for the reply. The file-extension that I am trying to upload is actually .tif (lowercase). Any other suggestions that can cause this .tif file not to be uploaded. Also, I can't get it to print out an error line, it acts like it actually uploaded the file.

    Thanks
    Rodrigo Olivares
    Small Business Owner (Web & Graphic Design)
    www.fearmydesign.com

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  5. #5
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    the way you have written your code makes it very easy to put debugging breaks in:
    PHP Code:
    /* debug 1 */
    echo '<p>Extension: '$ext .'</p>';
    /***********/
       
    if(!in_array($ext,$allowed_filetypes))
          die(
    '<h1 align="center" class="welcome">The file you attempted to upload is not allowed.</h1>');
     
    /* debug 2 */
    echo '<p>filesize: '$_FILES['userfile']['tmp_name'] .'</p>';
    /***********/
       
    if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
          die(
    '<h1 align="center" class="welcome">The file you attempted to upload is too large.</h1>');
    /* this if() is irrelevant as YOU shoul have sorted that out! */
       
    if(!is_writable($upload_path))
          die(
    '<h1 align="center" class="welcome">You cannot upload to the specified directory, please CHMOD it to 777.</h1>');

       if(
    move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path $filename))
    /* debug 3 */
    echo '<p>'$upload_path $filename .'</p>';
    /***********/ 
    Check the $ext is what you think it should be and the other variables are there.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....


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
  •