Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    RRT
    RRT is offline
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Why will this file upload not work?

    Hi, I am trying to create a php file upload page where JPG images can be uploaded to the server. Below are the 2 php pages that should give this functionality. For some reason it is not working. Do you have any idea as to why this file upload not work?:

    upload.php
    PHP Code:
    <?php
    echo "<HTML>\n";
    echo 
    "<TITLE>Upload a file</TITLE>\n";
    echo 
    "<BODY>\n";
    echo 
    "<form action=\"do_upload.php\" method=\"post\" enctype=\"multipart/form-data\">\n";
    echo 
    "<P><STRONG>File to Upload:</STRONG><BR>\n";
    echo 
    "<input type=\"file\" name=\"file\">\n";

    echo 
    "<P><input type=\"submit\" name=\"submit\" value=\"Upload image\">\n";
    echo 
    "<input type=\"reset\" value=\"reset\" name=\"Reset\">";

    echo 
    "</form>";
    echo 
    "</body>";
    echo 
    "</html>";
    do_upload.php:
    PHP Code:
    <?php
    $img1_name 
    $_POST[img1];
    $img1_type $_POST[file];

    echo 
    " " trim($img1) . "\n";
    echo 
    " " trim($img1_name) . "\n";
    echo 
    "\n";
    echo 
    "<P><HR>";

     if (
    is_uploaded_file($img1_name)) {
        
    move_uploaded_file($img1_name"/var/www/html/acme/$img1_name") or die("<H3><FONT COLOR=RED>ERROR 2 : (error uploading image file to portal)</H3><HR><FONT COLOR=BLACK> <LI>Couldn't copy the file. Please contact Technical Support.");
     }
     else {
        die(
    "<H3><FONT COLOR=RED>ERROR 1 : (error uploading image file to portal)</H3><HR><FONT COLOR=BLACK> <LI>No input file specified. Select a file first before clicking the <I>Upload File</I> button.");
         }
    ?> 

    <HTML>
    <HEAD>
    <TITLE>Upload Status...</TITLE>
    </HEAD>
    <BODY>
    <H1>Success!</H1>
    </BODY>
    </HTML>
    Do you see any syntax or logic errors as to why this isn't working properly? During the 'if-else' block, it's just going to the 'die' section where it says "ERROR 1".
    Last edited by RRT; 02-28-2012 at 08:11 PM. Reason: typos

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,324
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    First of all files are stored in the $_FILES array - not $_POST. That is your first error.

    Your second error is assuming that move_uploaded_file() was successful and outputting a success message even if it failed. The function returns a boolean result so you can use it in an if() conditional to check and act accordingly.

    At the top of your script, do this:
    var_dump($_FILES);

    That will let you see what is there and how to access it.

    Also for your first script, you can turn that into a html page - you don't need to echo it line by line. If you want to echo it via php then you can do this:

    echo <<<STOP
    <html>
    <!--your html here-->
    </html>
    STOP;

    That will make PHP echo out everything until the word STOP
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    RRT
    RRT is offline
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation

    OK, so you are saying to modify the second file that I posted to this:

    PHP Code:
    <?php
    $img1name 
    $_FILES['img1'];


    var_dump($_FILES);
    echo 
    "NAME: " "'%s'" "\n"pg_escape_string(trim($img1name));
    echo 
    "\n";
    echo 
    "<P><HR>";

     if (
    is_uploaded_file($img1name)) {
        
    move_uploaded_file($img1name"/var/www/html/portal/acme/images/$img1_name") or die("<H3><FONT COLOR=RED>ERROR 2 : (error uploading image file to portal)</H3><HR><FONT COLOR=BLACK> <LI>Couldn't copy the file. Please contact Technical Support.");
     }
     else {
        die(
    "<H3><FONT COLOR=RED>ERROR 1 : (error uploading image file to portal)</H3><HR><FONT COLOR=BLACK> <LI>No input file specified. Select a file first before clicking the <I>Upload File</I> button.");
         }
    ?>
    This is the result that I get from this new code change:

    array(1) { ["img1"]=> array(5) { ["name"]=> string(10) "Winter.jpg" ["type"]=> string(10) "image/jpeg" ["tmp_name"]=> string(14) "/tmp/php1rEx6y" ["error"]=> int(0) ["size"]=> int(105542) } } NAME: '%s' Array

    ERROR 1 : (error uploading image file to portal)
    No input file specified. Select a file first before clicking the Upload File button.
    Last edited by RRT; 02-29-2012 at 05:26 PM. Reason: typos

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,324
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    You should be accessing the file like this:
    $img1name = $_FILES['img1']['tmp_name'];

    Let me explain how this all works..

    When you upload a file to a php script, php stores its details in the $_FILES array whilst storing the file as a temporary file with a temporary name. In the files array you will find a multidimensional array which will contain the temp name, the file size, the real name etc.

    When accessing the uploaded file, you need to address it by its temporary name until you move it to its new location and save it under its original (or any other) file name.

    Currently you're doing this:
    $img1name = $_FILES['img1'];

    Which will basically set $img1name to an array of the files details. You can't then move an array using move_uploaded_file() to its new location - an array is a chunk of memory not a file!

    This is something like what you should be doing:
    isset($_FILES)
    !empty($_FILES)
    is_uploaded_file($_FILES['img1']['tmp_name'])
    move_uploaded_file($_FILES['img1']['tmp_name'], $_FILES['img1']['name'])
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    RRT (03-01-2012)

  • #5
    RRT
    RRT is offline
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Wow, your insight has been very helpful, I *really* appreciate it! One quick follow-up question: if I want the 'move_uploaded_file' function to be able to move the file being uploaded to a sub-directory or some other directory, how would I specify that within the syntax:

    PHP Code:
    move_uploaded_file($_FILES['img1']['tmp_name'], $_FILES['img1']['name']) 

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,324
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    Something like this:

    PHP Code:
    move_uploaded_file($_FILES['img1']['tmp_name'], 'some_other_directory/' .$_FILES['img1']['name']); 
    Or:
    PHP Code:
    move_uploaded_file($_FILES['img1']['tmp_name'], "some_other_directory/$_FILES[img1][name]"); 
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    RRT (03-02-2012)

  • #7
    RRT
    RRT is offline
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Thumbs up

    AWESOME, it worked! You rock!!! Thanks again.


  •  

    Tags for this Thread

    Posting Permissions

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