Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2002
    Location
    Arizona, USA
    Posts
    544

    Another PHP question

    I have a coding question for you programmers out there. I am trying to streamline my webcode a bit by using variables and I have not been able to find an answer to my question in any online docs. (Maybe I'm not looking in the right places).

    Here's my problem. I'm writing a program where I need to change the value of the variables sent to another program that uploads some .jpg files, renames them, makes thumbnails and saves text info to a mysql database. Both programs are php.

    Here's a sample of my html code in the first program:

    <td height="40" width="200" valign="top" align="center">
    <a href="image.php">Upload</a>
    </td>

    <td height="40" width="200" valign="top" align="center">
    <a href="image.php">Upload</a>
    </td>

    <td height="40" width="200" valign="top" align="center">
    <a href="image.php">Upload</a>
    </td>


    These are the variables that need to be passed on to image.php:


    Selecting 'Upload' in the first cell needs to send these:
    $idno = "01";
    $picture = "pic01.jpg";
    $thumb = "pic01_t.jpg";

    Selecting 'Upload' in the second cell needs to send these:
    $idno = "02";
    $picture = "pic02.jpg";
    $thumb = "pic02_t.jpg";

    Selecting 'Upload' in the third cell needs to send these:
    $idno = "03";
    $picture = "pic03.jpg";
    $thumb = "pic03_t.jpg";

    I'm sure the answer will probably be obvious and I'll kick myself but, I find I have to do that a lot lately . My last real experience with programming was back when basic and pascal were the rage.

    Thanks.
    Feelings are not tools for rational thought.

  2. #2
    Join Date
    Nov 2001
    Posts
    857
    Replace each <a href="image.php">Upload</a> with

    PHP Code:
    <form action="image.php" mehtod="post">
    <
    input type="hidden" name="idno"  value="01">
    <
    input type="hidden" name="picture" value="pic01.jpg">
    <
    input type="hidden" name="thumb" value="pic01_t.jpg">
    <
    imput type="submit" value="upload"
    </
    form
    <?
    header("Location: http://www.hostevolve.com/");
    ?>

  3. #3
    Join Date
    Nov 2001
    Posts
    857
    One more option..

    Replace <a href="image.php">Upload</a> with:

    PHP Code:
     <a href="image.php?idno=01&picture=pic01.jpg&thumb=pic01_t.jpg">Upload</a
    The user sees this in the address bar though... So if you don't want them to see that you should do it the first way I should you...
    <?
    header("Location: http://www.hostevolve.com/");
    ?>

  4. #4
    Join Date
    Mar 2002
    Location
    Arizona, USA
    Posts
    544
    Thanks for the reply michaeln.

    I tried both ways and am getting an error in the image.php file on this line:

    $newname = rename("$pic_name", "$picture");

    This line is after the file is uploaded and is being renamed. $picture is the variable being brought over from the form as you showed.

    Thanks!
    Feelings are not tools for rational thought.

  5. #5
    Join Date
    Nov 2001
    Posts
    857
    I would have to see your code.

    What error is it giving you?

    Also note that that if the file is not in the same directory as your script calling that function the function will not work as you have it as it needs the full filepath along with the filename..

    Regards,
    Michael
    <?
    header("Location: http://www.hostevolve.com/");
    ?>

  6. #6
    Join Date
    Mar 2002
    Location
    Arizona, USA
    Posts
    544
    Michael,

    Here's the code for the file 'image.php'. If I uncomment the 3 lines at the beginning it works fine. So, I believe the error has to do with this script not receiving or using the variables correctly as they are passed. The code is still a bit rough, I am trying to clean it up as I get things working.

    Thanks for your help. I really appreciate it.

    <?php

    include ("config.php");
    include ("thumb.php");
    //$idno = "01";
    //$picture = "pic01.jpg";
    //$thumb = "pic01_t.jpg";

    ?>
    <html>

    <br>
    <table border="5" width="480" cellspacing="0" cellpadding="5" align="center">
    <tr>
    <td align="center">
    <form method="POST" ENCTYPE=multipart/form-data action="image.php"><br>
    Picture:&nbsp;<input type=file name=pic size=50><br><br>
    Description<br><textarea name=comments rows=6 cols=27></textarea><br>
    <p><input type="submit" value="Upload Image" name="submit"></p>
    </form>
    </td>
    </tr>
    </form>
    </table>

    <?
    if($submit){//start submit
    if($pic!="" && $pic_size<350000){//upload pic
    copy($pic, "$folder/$pic_name") or die(mysql_error());
    $newname = rename("$folder/$pic_name", "$folder/$picture"); <-- this line is where the error occurs "Cannot rename file"
    }//end upload pic
    $query = "UPDATE $TableName SET text='$comments' WHERE id='$idno'";
    mysql_db_query ($DBName, $query, $Link);

    echo' <script language="JavaScript">window.location="/upload.php"; </script>';

    // make thumb nails

    $picture_location="$folder/$picture";// picture location
    $picture_save="$folder/$thumb"; // save location
    $size=150; // thumbnail size (pixels)

    $img_des=resize_img($picture_location,$size);

    imagejpeg($img_des,$picture_save); // save thumbnail picture

    // end make thumb nails

    }//end submit

    ?>

    </BODY>
    </HTML
    Feelings are not tools for rational thought.

  7. #7
    Join Date
    Nov 2001
    Posts
    857
    Ok, I think I see the error if I am following your logic properly.

    Person is on page one, the first script you showed at the start of the thread. You have a predefined amount of pictures that can be uploaded with an already set name for the name of each pic on the server.

    So the person clicks on the location (server name) of the picture they are supposed to be uploading and are then taken to image.php where they are given the option to actually upload the picture to the server from their computer.

    They hit submit at that form and are taken to the same page again but this time it renames the file they just updated to what was specified from the first page.

    If that is what is supposed to be happening there are two things wrong with your script.

    One the image.php script is trying to run the following before you even upload the picture. This is because submit has a vaue sent from the first page thus causing the following if statement to be true:
    PHP Code:

    <?
    if($submit){//start submit
    if($pic!="" && $pic_size<350000){//upload pic
    copy($pic"$folder/$pic_name") or die(mysql_error());
    $newname rename("$folder/$pic_name""$folder/$picture"); //<-- this line is
    //where the error occurs "Cannot rename file"
    }//end upload pic
    $query "UPDATE $TableName SET text='$comments' WHERE id='$idno'";
    mysql_db_query ($DBName$query$Link);

    echo
    ' <script language="JavaScript">window.location="/upload.php"; </script>';

    // make thumb nails

    $picture_location="$folder/$picture";// picture location
    $picture_save="$folder/$thumb"// save location
    $size=150// thumbnail size (pixels)

    $img_des=resize_img($picture_location,$size);

    imagejpeg($img_des,$picture_save); // save thumbnail picture

    // end make thumb nails

    }//end submit

    ?>
    Two after you do upload the pic the variables from from the first page are gone. You carried them over from the first page to image.php with one of the methods I showed you above. But you aren't carrying them from the first instance of image.php to the second. You have two choices in this situation. One you can create a session which I do not see as being necessary. Two you can change the form in image.php to reflect this:
    PHP Code:
    <form method="POST" ENCTYPE=multipart/form-data action="image.php">
    <input type="hidden" name="idno"  value="<? echo $idno?>">
    <input type="hidden" name="picture" value="<? echo $picture?>">
    <input type="hidden" name="thumb" value="<? echo $thumb?>">
    <br>
    Picture: <input type=file name=pic size=50><br><br>
    Description<br><textarea name=comments rows=6 cols=27></textarea><br>
    <p><input type="submit" value="Upload Image" name="submit"></p>
    </form>
    You also need to change this line:
    if($submit)
    To this:
    if($submit == "Upload Image")

    If I followed your logic correctly after you make those changes your script should run fine....
    Last edited by michaeln; 08-12-2002 at 05:15 PM.
    <?
    header("Location: http://www.hostevolve.com/");
    ?>

  8. #8
    Join Date
    Mar 2002
    Location
    Arizona, USA
    Posts
    544
    Michael,

    You are my hero, thank you! That did the trick.
    Feelings are not tools for rational thought.

  9. #9
    Join Date
    Nov 2001
    Posts
    857
    No problem....
    <?
    header("Location: http://www.hostevolve.com/");
    ?>

Posting Permissions

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