SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: image upload

  1. #1
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)

    image upload

    I'm dying in troubleshooting in this thing. I did it already in the other page, I keep on comparing but I can't find the problem. It is just a simple image upload..

    html
    Code:
    <form action = "image_act.php" method = "post" target = "stud_iframe" >
    		<label for="file">Filename:</label>
    		<input type="file" name="images" id="images" required/> 
    		<input type = 'submit' name = 'submit' value = "Submit" />
    		<br/>
    			
    </form>	
    		<iframe class = "stud_frame" name = "stud_iframe" src="image_act.php" style="background-color: #dfdfdf"></iframe>
    image_act.php
    Code:
    if (isset($_POST['submit'])) 
    {
    echo $img_name = addslashes($_FILES['images']['name']);
    echo $tmp_name = $_FILES['images']['tmp_name'];
    echo $img_size = $_FILES['images']['size'];
    echo $img_type = $_FILES['images']['type'];
    echo $max_size = 2097152;
    echo $extension = strtolower(substr($img_name, strpos($img_name, '.') + 1));
    echo $img_error = $_FILES['image']['error'];
    }
    I can't see any result after printing this all ..

  2. #2
    SitePoint Enthusiast
    Join Date
    Oct 2011
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Remember to add the following attribute to your form or file input fields will be ignored.

    enctype="multipart/form-data"

  3. #3
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    This line
    PHP Code:
    echo $img_name addslashes($_FILES['images']['name']); 
    does not display the value of $img_name. What it does is display the result of the = operation, which is TRUE.
    Try
    PHP Code:
    $img_name addslashes($_FILES['images']['name']);
    echo 
    $img_name
    The same goes for the other echoes in the code you posted.

  4. #4
    SitePoint Enthusiast
    Join Date
    Oct 2011
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I recommend using var_dump for debug output, you will get a more insight in to what data you are actually getting and what type it is.
    What guido2004 said is also correct. I think you also need the right enctype in your form attributes.

  5. #5
    SitePoint Enthusiast jakub_polak's Avatar
    Join Date
    Jan 2012
    Location
    Slovakia
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    This line
    PHP Code:
    echo $img_name addslashes($_FILES['images']['name']); 
    does not display the value of $img_name. What it does is display the result of the = operation, which is TRUE.
    Try
    PHP Code:
    $img_name addslashes($_FILES['images']['name']);
    echo 
    $img_name
    The same goes for the other echoes in the code you posted.
    If you try this:

    PHP Code:
    echo $a 'Hello World!'
    then the result on screen will be "Hello World!", what happens here is that:
    1.) 'Hello World' is assigned to $a
    2.) content of $a is displayed on screen

    The problem in the form is missing enctype which should be:

    PHP Code:
    enctype="multipart/form-data" 
    For debugging purposes you should use something like this:

    PHP Code:
    echo '<pre>'var_dump($_POST); echo '</pre>';
    echo 
    '<pre>'var_dump($_FILES); echo '</pre>'
    I don't recommend using this:

    PHP Code:
    isset($_POST['submit']) 
    'submit' is a key of $_POST array so in my opinion you should use function array_key_exists.

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by jakub_polak View Post
    If you try this:

    PHP Code:
    echo $a 'Hello World!'
    then the result on screen will be "Hello World!", what happens here is that:
    1.) 'Hello World' is assigned to $a
    2.) content of $a is displayed on screen
    You're absolutely right. My bad.

  7. #7
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    $_FILES['image']['error'];

    Is the var name 'images' or 'image'?

    The first thing to do is check for an error number upload error codes

    Relying on isset($_POST['submit']) used to be prone to failure too, as in some browsers the user hitting the Enter key submits the form but not the submit button value IIRC.

    Something like:

    PHP Code:
    if( $_FILES['images']['error'] > ) {
    // fail, redirect, exit

    }else{
    // at least you should actually have a file to handle from here


    ... might be a better way to go.


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
  •