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 13 of 13
  1. #1
    New Coder
    Join Date
    Feb 2007
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Upload script: IE vs Firefox

    Ok, so i've got a script that's kind of for a friend to make new posts/update his site easily, and from anywhere.

    So i made a script that inserts the content from certain fields into a mysql database. One of these is a filename.

    so the script inserts the information from the input type=file that i have, np.

    The problem is this:
    Firefox will input filename.jpg
    IE will input c:\full\friggin\path\to\the\filename.jpg

    My question is, should i be putting something in my code that helps IE just input the filename.jpg and not the path?

  • #2
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Sure.

    Although I don't think you need to be dealing with this information in PHP. The filename in the $_FILES array should not contain the path fo the filename.

    PHP Code:
    if (strpos($filename"\\") !== false) {
        
    $filename substr(strrchr($filename"\\"), 1);

    Last edited by aedrin; 02-23-2007 at 04:37 PM.

  • #3
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Use the $_FILES array.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,108
    Thanks
    2
    Thanked 326 Times in 318 Posts
    Even though IE shows the full path in the form input field, the ['name'] element of the $_FILES array only contains the actual file name.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #5
    New Coder
    Join Date
    Feb 2007
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, i've got the actual upload script part down, i think.

    What about the information being inserted into mysql?

    If i submit an image using FireFox, then use another .php to view the contents of that table, it'll pull imagename.jpg

    But if i submit an image with IE, then use the other .php script, it pulls c:\full\path\to\imagename.jpg

    So what i think is going on is IE will insert the whole path to mysql, fire fox only inserts the file name into mysql.
    PHP Code:
    $date $_POST['date'];
    $title $_POST['title'];
    $thumb $_POST['thumb'];
    $thcom $_POST['thcom'];
    $story $_POST['story'];
    $poster $_POST['poster'];

    $query "INSERT INTO news (news_datee, news_timee, news_title, news_th_path, news_th_com, news_story, news_poster) VALUES (NOW(), NOW(), '$title', '$thumb', '$thcom', '$story', '$poster')";
    mysql_query($query) or die('Error, insert query failed');

    mysql_close(); 
    news_th_path is the name of the table where my imagename is being stored... i didn't think really hard when i named it, and i'll probably rename it. My point is, news_th_path shouldn't contain the whole path, just the imagename.

    I just don't get why IE is inserting the whole path and firefox isn't.

  • #6
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,108
    Thanks
    2
    Thanked 326 Times in 318 Posts
    You would need to post your form code as none of the variables you show or insert into the database are a result of the file upload field.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #7
    New Coder
    Join Date
    Feb 2007
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <form method="post">
    <table border="1" cellspacing="1" cellpadding="2">
    <tr> 
    <td>Post Title</td>
    <td><input name="title" type="text" size="100" id="title"></td>
    </tr>
    <tr> 
    <td>Thumbnail Image</td>
    <td><input name="thumb" type="file" size="100" id="thumb"></td>
    </tr>
    <tr> 
    <td>Thumbnail Comment</td>
    <td><input name="thcom" type="text" size="100" id="thcom"></td>
    </tr>
    <tr> 
    <td>Article Text</td>
    <td><textarea name="story" rows="20" cols="125" type="text" id="story"></textarea></td>
    </tr>
    <tr> 
    <td>Name</td>
    <td><input name="poster" type="text" size="100" id="poster"></td>
    </tr>
    <tr> 
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr> 
    <td width="100">&nbsp;</td>
    <td><input name="add" type="submit" id="add" value="Add New Post"></td>
    </tr>
    </table>
    </form>

  • #8
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Are you actually uploading the file, or just using the entry field type="file" as an easy way of entering a filename?

    Up to this point we've all been assuming you are uploading a file and then storing the filename in your table, but your form isn't correct for uploading.

    Just in case you are doing a file upload, you need to do like this:

    http://us2.php.net/manual/en/features.file-upload.php

  • #9
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,108
    Thanks
    2
    Thanked 326 Times in 318 Posts
    My thoughts exactly.

    If you are actually uploading, your <form ... tag will need the following -
    Code:
    enctype="multipart/form-data"
    Once you do this, the $_POST['thumb'] will go away and your problem will be solved, because you will then need to use the proper $_FILES... ['name'] element in your insert query.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #10
    New Coder
    Join Date
    Feb 2007
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    it's always something simple with me, haha.

    Ok, so, if i want both functions to occur at the same time?

    For instance if i want to insert the filename from the type=file into mysql with the rest of the form, then upload the file, would adding the enctype enable that?

    this is what i have:
    PHP Code:
    $date $_POST['date'];
    $title $_POST['title'];
    $thumb $_POST['thumb'];
    $thcom $_POST['thcom'];
    $story $_POST['story'];
    $poster $_POST['poster'];

    $query "INSERT INTO news (news_datee, news_timee, news_title, news_th_path, news_th_com, news_story, news_poster) VALUES (NOW(), NOW(), '$title', '$thumb', '$thcom', '$story', '$post er')";
    mysql_query($query) or die('Error, insert query failed');

    mysql_close();

    $target_path "images1/";
    $target_path $target_path basename($_FILES['thumb']['name']); 

    // start upload
    if(move_uploaded_file($_FILES['thumb']['tmp_name'], $target_path)) {
        echo 
    "The file ".  basename($_FILES['thumb']['name']). 
        
    " has been uploaded.<br>";

    else{
        echo 
    "There was an error uploading the file!<br>";
    }
    // end upload 
    Last edited by MnilinM; 02-23-2007 at 06:06 PM. Reason: added my code

  • #11
    New Coder
    Join Date
    Feb 2007
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok, so...

    If i add the enctype code to my form, it will upload the file, but it won't add the filename to mysql...

    I had a thought, should i be finding a way to use one variable/reference for both the sql insertion and the upload?

    If so, which should i use? would something like
    PHP Code:
    $_POST['thumb']['name'
    work?

    then i can use a variable like
    PHP Code:
    $thumbname $_POST['thumb']['name'
    in both the sql insertion and the upload script?

  • #12
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    If you have the upload process working, then inserting a row into a MySQL table using the value of $_FILES['thumb']['name'] shouldn't require any special $_POST variable-- just use $_FILES['thumb']['name'] in your insert query.

  • #13
    New Coder
    Join Date
    Feb 2007
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    excellent! we're up and running.

    Thanks a ton to everyone who puts up with the newb questions.

    It's extremely appreciated!


  •  

    Posting Permissions

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