Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

gavd

macrumors 6502a
Original poster
Jan 30, 2006
602
2
OK, this is a bit of a long post, but it's because I've copied my code in!
I'm learning PHP from a Wrox published book, but I can't get the following code to work.
It works up to the if (move_uploaded_file()) point, but doesn't seem to do anything from there. It's not erroring because the page loads all the HTML code, but the PHP variables hold no values.

Can anyone shed any light?

Thanks

<?php
//connect to the database
$link = mysql_connect("localhost", "*****", "*****")
or die("Could not connect: " . mysql_error());
mysql_select_db("moviesite", $link)
or die (mysql_error());

//make variables available
$image_caption = $_POST['image_caption'];
$image_username = $_POST['image_username'];
$image_tempname = $_FILES['image_filename']['name'];
$today = date("Y-m-d");

//upload image and check for image type
//make sure to change your path to match your images directory
//$ImageDir ="c:/Program Files/Apache Group/Apache2/test/images/";
$ImageDir ="/Users/gavdent/Sites/images/";
$ImageName = $ImageDir . $image_tempname;

if (move_uploaded_file($_FILES['image_filename']['tmp_name'],
$ImageName)) {
//get info about the image being uploaded
list($width, $height, $type, $attr) = getimagesize($ImageName);

switch ($type) {
case 1:
$ext = ".gif";
break;
case 2:
$ext = ".jpg";
break;
case 3:
$ext = ".png";
break;
default:
echo "Sorry, but the file you uploaded was not a GIF, JPG, or " .
"PNG file.<br>";
echo "Please hit your browser's 'back' button and try again.";
}
//insert info into image table
$insert = "INSERT INTO images
(image_caption, image_username, image_date)
VALUES
('$image_caption', '$image_username', '$today')";
$insertresults = mysql_query($insert)
or die(mysql_error());
$lastpicid = mysql_insert_id();
$newfilename = $ImageDir . $lastpicid . $ext;
rename($ImageName, $newfilename);
}
?>
<html>
<head>
<title>Here is your pic!</title>
</head>
<body>
<h1>So how does it feel to be famous?</h1><br><br>
<p>Here is the picture you just uploaded to our servers:</p>
<img src="images/<?php echo $lastpicid . $ext; ?>" align="left">
<strong><?php echo $image_name; ?></strong><br>
This image is a <?php echo $ext; ?> image.<br>
It is <?php echo $width; ?> pixels wide
and <?php echo $height; ?> pixels high.<br>
It was uploaded on <?php echo $today; ?>.
</body>
</html>
 

superbovine

macrumors 68030
Nov 7, 2003
2,872
0
FYI: next time use the php tags

PHP:
<?php
//connect to the database
$link = mysql_connect("localhost", "*****", "*****")
or die("Could not connect: " . mysql_error());
mysql_select_db("moviesite", $link) 
or die (mysql_error());

//make variables available
$image_caption = $_POST['image_caption'];
$image_username = $_POST['image_username'];
$image_tempname = $_FILES['image_filename']['name'];
$today = date("Y-m-d");

//upload image and check for image type
//make sure to change your path to match your images directory
//$ImageDir ="c:/Program Files/Apache Group/Apache2/test/images/";
$ImageDir ="/Users/gavdent/Sites/images/";
$ImageName = $ImageDir . $image_tempname;

if (move_uploaded_file($_FILES['image_filename']['tmp_name'], 
$ImageName)) {
//get info about the image being uploaded
list($width, $height, $type, $attr) = getimagesize($ImageName);

switch ($type) {
case 1:
$ext = ".gif";
break;
case 2:
$ext = ".jpg";
break;
case 3:
$ext = ".png";
break;
default:
echo "Sorry, but the file you uploaded was not a GIF, JPG, or " .
"PNG file.<br>";
echo "Please hit your browser's 'back' button and try again.";
}
//insert info into image table
$insert = "INSERT INTO images
(image_caption, image_username, image_date)
VALUES
('$image_caption', '$image_username', '$today')";
$insertresults = mysql_query($insert)
or die(mysql_error());
$lastpicid = mysql_insert_id();
$newfilename = $ImageDir . $lastpicid . $ext;
rename($ImageName, $newfilename);
}
?>
<html>
<head>
<title>Here is your pic!</title>
</head>
<body>
<h1>So how does it feel to be famous?</h1><br><br>
<p>Here is the picture you just uploaded to our servers:</p>
<img src="images/<?php echo $lastpicid . $ext; ?>" align="left">
<strong><?php echo $image_name; ?></strong><br>
This image is a <?php echo $ext; ?> image.<br>
It is <?php echo $width; ?> pixels wide 
and <?php echo $height; ?> pixels high.<br>
It was uploaded on <?php echo $today; ?>.
</body>
</html>
 

gavd

macrumors 6502a
Original poster
Jan 30, 2006
602
2
HTML Code

Sure, here it is. Thanks for the PHP tags tip as well. Didn't realise you could do that.
Code:
<html>
<head>
<title>Upload your pic to our site!</title>
</head>
<body>
<form name="form1" method="post" action="check_image.php" 
    enctype="multipart/form-data">
<table border="0" cellpadding="5">
  <tr>
    <td>Image Title or Caption<br>
      <em>Example: You talkin' to me?</em></td>
    <td><input name="image_caption" type="text" id="item_caption" size="55" 
          maxlength="255"></td>
  </tr>
  <tr>
    <td>Your Username</td>
    <td><input name="image_username" type="text" id="image_username" size="15" 
          maxlength="255"></td>
  </tr>
    <td>Upload Image:</td>
    <td><input name="image_filename" type="file" id="image_filename"></td>
  </tr>
</table>
<br>
<em>Acceptable image formats include: GIF, JPG/JPEG, and PNG.</em>
<p align="center"><input type="submit" name="Submit" value="Submit">
   
  <input type="reset" name="Submit2" value="Clear Form">
</p>
</form>
</body>
</html>
 

superbovine

macrumors 68030
Nov 7, 2003
2,872
0
oh... heh it also the stupid things...

you need to change the permission on the images directory to 767 from terminal

Code:
cd Sites
chmod 767 images

or just cmd click or right the directory then get info and under the ownership & permissions info set it to.

You can read & write
read & write
group:
no access

others read & write
 

zimv20

macrumors 601
Jul 18, 2002
4,402
11
toronto
gavd said:
It works up to the if (move_uploaded_file()) point, but doesn't seem to do anything from there.
i haven't looked at your code, but it's possible the problem is on the server end.

i'd written some php to upload files to one of my sites and it failed on the move. i went round and round with the hosting company (hostgator.com, and they were actually pretty awesome about keeping at it) and, suddenly, it all worked.

they said they made a change on their end, but repeated attempts to ask what it was didn't net me an answer (the not so awesome part).

for the next few days, sometimes it would work and sometimes it wouldn't. now it mostly works, but sometimes it still fails. and i have no idea why.

work up the smallest test case you can that still fails and see if your hosting company can reproduce the problem.
 

zimv20

macrumors 601
Jul 18, 2002
4,402
11
toronto
if this helps, here's my small test case. you'll have to edit the php file to match your particular file destination.

Code:
<html>
<head>
<title>upload test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<form enctype="multipart/form-data" action="upload_test.php" method="post">
        <hidden name="MAX_FILE_SIZE" value=15000 />
        <input name="user_file" type="file" />
        <br>

        <input type="submit" value="Submit" />
</form>

</body>
</html>



PHP:
<html>
<head>
<title>upload test php</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<? 
//----------------------------------------------------------------
//
// %Z%%M% %I% %G%
//
// upload_test
//
// sxz 041219
//
//----------------------------------------------------------------

/*
session_start();
header("Cache-control: private");
*/
?>

<body>
<br />

<?php
        //-----------------------------------------------------------
        // rejectFile
        //-----------------------------------------------------------

        function rejectFile($reason)
        {
?>
                <h3>
                        Failure
                </h3>
                <p>
<?php
                        echo $reason;
                        exit();
?>
                </p>
<?php
        }

        //-----------------------------------------------------------
        // baseMusicPath
        //-----------------------------------------------------------

        function baseMusicPath()
        {
                return "members/upload_test";
        }

        //-----------------------------------------------------------
        // wholeMusicPath
        //-----------------------------------------------------------

        function wholeMusicPath()
        {
                return $_SERVER['DOCUMENT_ROOT'] . "/" . baseMusicPath();
        }

        //-----------------------------------------------------------
        // processUploadedFile
        //-----------------------------------------------------------

        function processUploadedFile($orig_filename, $tmp_filename)
        {
                // ensure uploaded file is good

                if (!is_uploaded_file($tmp_filename))
                {
                        rejectFile("Danger: uploaded file " . $tmp_filename
                                . " not uploaded correctly");
                        exit;
                }

                // grab some data so we can determine where to put
                // this thing

                $path = wholeMusicPath();
                $dst = $path . "/" . $orig_filename;

                $src = $tmp_filename;   // for php library function xfers

                if (!move_uploaded_file($src, $dst))
                {
                        rejectFile("Could not move " . $src . " to destination " . $dst);
                        exit;
                }

                return $dst;
        }

        //-----------------------------------------------------------
        // main
        //-----------------------------------------------------------

        // grab needed info for our uploaded file

        $orig_filename = $_FILES['user_file']['name'];
        $tmp_filename = $_FILES['user_file']['tmp_name'];

/*
echo "orig_filename : " . $orig_filename; newline();
echo "tmp_filename : " . $tmp_filename; newline();
*/

        echo "attempting to move " . $orig_filename . " from " . $tmp_filename;
        echo "<br><br>";

        $url = processUploadedFile($orig_filename, $tmp_filename);

        $msg = "File " . $orig_filename . " uploaded to " . $url;
?>

<h3>
        Success
</h3>
<p>
        <?php echo $msg; ?>
</p>

</body>
</html>
 

gavd

macrumors 6502a
Original poster
Jan 30, 2006
602
2
Progress!

OK, thanks to superbovine I can now upload the image to the images folder within my sites folder.

However, the html which should then display the image doesn't!

I have also tried to load the image using preview from the directory, but I get the following message...

5.jpg
Couldn't open the file. It may be corrupt or a file format that Preview doesn't recognize.

Anyone got any ideas?

Thanks for your help so far!
 

zimv20

macrumors 601
Jul 18, 2002
4,402
11
toronto
download the image and see if you can open it on your mac. or even run diff on it. if it's changed, then you know something happened when it was uploaded or moved.

if it's not changed, then you can start looking at the browser display technology.
 

gavd

macrumors 6502a
Original poster
Jan 30, 2006
602
2
The images I've been using to test the code are less than 100K so it's not the size that's the issue.
I'm not sure if it could be the renaming of the image which might cause the problem. Not sure what else would cause it to corrupt and not let me be able to open it. :confused:
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.