#1
  1. Full Access
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jun 2000
    Location
    London, UK
    Posts
    2,019
    Rep Power
    17
    In a multipart html form, I am trying to submit an image in GIF format to this script, along with some other info in text areas and whatnot that will get inserted into the database, and the GIF will get copied to a directory. However, something in this script appears to be unsetting the $file variable, which is very distressing as the whole process then fails. Can anyone see why the $file variable is getting unset? Here's my PHP code:

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>

    <?

    MYSQL_CONNECT(localhost,user,pass) OR DIE("Unable to connect to database");

    @mysql_select_db(db) or die("Unable to select database");

    $id = uniqid(date("U"));

    /*

    $channel = "0";

    if (isset($file)) {

    $channel = "1";

    }

    $channel = "1";
    */
    $time = (date("G:i"));
    $date = (date("dS F, Y"));

    if ($file_type == "image/gif") {

    $query = "INSERT INTO articles (name,email,club,problem,solution,id,section,date,time,subject,explanation,coaching,channel) VALUES ('$name', '$email', '$club', '$problem', '$solution', '$id', '$section', '$date', '$time', '$subject', '$explanation', '$coaching', '$channel')";

    $result = mysql_query ($query);

    if ($result) {

    copy($file,"/usr/users/g25n36rj/public_html/irtest/images/$id.gif");
    echo "<META HTTP-EQUIV="Refresh" content="0;article.php3?section=$section">";

    /* Debug section
    print "Article added<br>.";
    print "The SQL statement was:<p> $query<br>";
    print "And the result was:<p> $result<br>";
    print "The section was $section<br>";
    print "Click <a href="ideas.php3?section=$section">here</a> to go to the section the article's in";
    */

    }

    else {

    print "Boog.<p>This was the query: <p>$query<p>And for some hopefully-apparent reason it wasn't inserted<p>MySQL said:<br>";
    echo mysql_error();
    }

    }

    else {

    print "Please submit your channel in GIF format. <br><a href="javascript:history.back();">Back</a><br>You submitted your channel in $file_type format<br>$file<p>";

    if (isset($file)) {

    echo "File is set";
    }
    else {

    echo "File is not set";

    }

    }

    MYSQL_CLOSE();

    ?>

    [/code]

    And here is the HTML form:

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>

    <html>
    <head>
    <title>Noticeboard</title>

    <link rel="stylesheet" type="text/css" href="../default.css">
    <script language="JavaScript" src="pop.js"></script>

    <body background="../bg.jpg" bgcolor="#FFFFFF" class="bg" link="#2799F1" vlink="#2799F1" alink="#2799F1">

    <form method="POST" enctype="multipart/form-data" action="article.php3?cmd=do_add">

    <table border="0">
    <tr>
    <td valign="middle">
    <h1 class=heading>Add a strategy</h1>
    </td>
    <td valign="baseline" align="left"><img src="shim.gif" width="15" height="1"></td>
    <td valign="baseline" align="left"><a href="javascript:newWindow('help.html', 'a', 480, 600, 1)"><img src="help.gif" width="73" height="23" border="0"></a></td>
    </tr>
    </table>
    <p><font face="Arial, Helvetica, sans-serif">To add a strategy, complete this
    form and click on Submit when you are finished.</font></p>
    <p><font face="Arial, Helvetica, sans-serif">Please note that if you choose
    to include a channel diagram, it must conform to this key:</font></p>
    <table border="0" cellspacing="3" cellpadding="0">
    <tr>
    <td>&nbsp;</td>
    <td>
    <p><img src="key.gif" width="356" height="293"></p>
    <p>&nbsp;</p>
    </td>
    </tr>
    <tr>
    <td><font face="Arial, Helvetica, sans-serif">Name:</font></td>
    <td>
    <input type="text" name="name" class=formelement>
    </td>
    </tr>
    <tr>
    <td><font face="Arial, Helvetica, sans-serif">Email:</font></td>
    <td>
    <input type="text" name="email" class=formelement>
    </td>
    </tr>
    <tr>
    <td><font face="Arial, Helvetica, sans-serif">Club/School:</font></td>
    <td>
    <input type="text" name="club" class=formelement>
    </td>
    </tr>
    <tr>
    <td><font face="Arial, Helvetica, sans-serif">Subject:</font></td>
    <td>
    <input type="text" name="subject" class=formelement>
    </td>
    </tr>
    <tr>
    <td><font face="Arial, Helvetica, sans-serif">Age range:</font></td>
    <td>
    <select name="section" class=formelement>
    <option value="youth">Youth Rugby</option>
    <option value="schools">Schools &amp; Colts</option>
    <option value="seniors">Seniors</option>
    </select>
    </td>
    </tr>
    <tr>
    <td valign="top"><font face="Arial, Helvetica, sans-serif">Problem:</font></td>
    <td>
    <textarea name="problem" cols="45" rows="10" class=formelement></textarea>
    </td>
    </tr>
    <tr>
    <td valign="top"><font face="Arial, Helvetica, sans-serif">Solution:</font></td>
    <td>
    <textarea name="solution" cols="45" rows="10" class=formelement></textarea>
    </td>
    </tr>
    <tr>
    <td><font face="Arial, Helvetica, sans-serif">Diagram:</font></td>
    <td>
    <input type="file" name="file" class=formelement>
    </td>
    </tr>
    <tr>
    <td valign="top"><font face="Arial, Helvetica, sans-serif">Diagram explanation:</font></td>
    <td>
    <textarea name="explanation" cols="45" rows="10" class=formelement></textarea>
    </td>
    </tr>
    <tr>
    <td valign="top"><font face="Arial, Helvetica, sans-serif">Coaching points::</font></td>
    <td>
    <textarea name="coaching" cols="45" rows="10" class=formelement></textarea>
    </td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>
    <input type="submit" value="Submit" name="submit" class="button">
    <input type="reset" value="Reset" name="reset" class="button">
    </td>
    </tr>
    </table>
    </form>

    </body></html>

    [/code]

    Please somebody HELPP!!!!! as I am currently in hell over this: i have tried everything, and all that i can determine is that the variable $file which holds my file is getting destroyed on the way to the script or not set at all for some reason, and I cannot see why not.

    Thanks all, you're great.

    --------------------------------------------
    Alex
    (http://www.alex-greg.co.uk)
  2. #2
  3. No Profile Picture
    Robert_J_Sherman
    Guest
    Devshed Newbie (0 - 499 posts)
    Alexgreg,

    I've done something similar to this.

    copying a 'browsed' file to the server, and storing a file name in the database.

    Granted our approach is a bit different, but I think I might be able to help.

    I'm having a very hard time reading the code you've posted, the text is very small in the browser.

    Nothing jumps out at me as being immediately wrong, but I do see a couple of wholes that you'll surely want ot cover up.

    You will surely want to add the hidden input for MAX_FILE_SIZE, so you can have some control over how large the image is that's uploaded.

    ALso, you'll probably want to accommodate more than just image/gif as there is also the image/gif89a mime.

    Here's the code I am using to verify the banner image was included in the file field.

    //STEP 1d: VERIFY THE FILE/COPY TO BANNERS DIRECTORY
    if ($banner && ($image_url != "none")) {
    $imagename = $image_url_name;
    $imagesize = $image_url_size;
    $imagetype = $image_url_type;
    $size = getImageSize($image_url);

    //verify appropriate MIME type
    $validmime = 0;
    for ($i = 0; $i < count($valid_mime); $i++) {
    if ($valid_mime[$i] == $imagetype) {
    $validmime = 1;
    }
    }
    if ($size[1] > $image_max_height) {
    //image is too tall//
    //what to do if the images height it to large
    }
    if ($size[0] > $image_max_width) {
    //image is too wide//
    //what to do if the image is too wide
    }
    if ($validmime == 0 &#0124; &#0124; $imagesize > $MAX_FILE_SIZE) {
    //appropriate mime not found//
    if ($validmime == 0) {
    //what to do if the mime isn't valid
    }
    //compare the selected upload file to the max value//
    if ($imagesize > $MAX_FILE_SIZE) {
    //what to do if the file size is too large
    }
    }
    if (!$missing_fields) {
    $imagename = date("U", time())."-".$imagename;
    if(!copy($image_url, $image_path.$imagename)) {
    //what to do if I can't copy or find the file
    }
    else {
    $image_url = $imagename;
    }
    }
    }

    Okay, note that the $validmime refers to an array of accepted image mime types.

    SO, the first thing I do is loop through that array and make sure the image_type is accepted.

    If it is, I then check the height and width of the image, to ensure it matches my acceped criteria.

    Remember this script is for upload "links" and "banners".. so we want to make sure the banners do not exceed 468x68 and are no larger than 15kb.

    Now assuming that all the checks are good, then we perform the copy of the file to the server, and create our own filename..

    WHich you will note is a combo of the "date" and the original filename. This is then setup for insertion into our database.

    Granted, in my script I made a few adjustments to for use on win32, which I've removed here for giving as an example.

    On my system, I've found that if I don't "stripslashes" from the $image_url field, that PHP claims is can't locate the file.. On the unix server, this issue doesn't
    occur...

    Anyway, I hope this can help you locate the problem...

    If not, send me an email, prehaps I can help further.



    ------------------
    SnR Graphics,
    Low Cost Hosting and Web Development.

Similar Threads

  1. input type=file - hiding the text box
    By playerone in forum HTML Programming
    Replies: 5
    Last Post: April 3rd, 2004, 08:00 AM
  2. input type=file value=""
    By mohamed sanad in forum HTML Programming
    Replies: 1
    Last Post: December 31st, 2003, 02:47 PM
  3. input type=file question
    By iso in forum HTML Programming
    Replies: 2
    Last Post: October 24th, 2003, 01:42 PM
  4. Browse files from input type=FILE?
    By ahmekar in forum ASP Programming
    Replies: 9
    Last Post: September 16th, 2003, 05:46 PM
  5. Replies: 2
    Last Post: June 5th, 2003, 05:18 PM

IMN logo majestic logo threadwatch logo seochat tools logo