PDA

View Full Version : PHP Question - From a Newbie!




gavd
Jan 30, 2006, 04:14 PM
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
Jan 30, 2006, 04:20 PM
FYI: next time use the php tags


<?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
Jan 30, 2006, 04:43 PM
Can we see the code from the page which I assume as a html form of some kind?

gavd
Jan 30, 2006, 04:51 PM
Sure, here it is. Thanks for the PHP tags tip as well. Didn't realise you could do that.
<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">
&nbsp;
<input type="reset" name="Submit2" value="Clear Form">
</p>
</form>
</body>
</html>

superbovine
Jan 30, 2006, 05:56 PM
oh... heh it also the stupid things...

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


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
Jan 31, 2006, 01:12 AM
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
Jan 31, 2006, 01:19 AM
if this helps, here's my small test case. you'll have to edit the php file to match your particular file destination.


<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>





<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
Jan 31, 2006, 12:19 PM
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
Jan 31, 2006, 01:27 PM
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.

superbovine
Jan 31, 2006, 09:13 PM
if your image over 8 meg that is the default limit for move_uploaded_file.

gavd
Feb 3, 2006, 03:19 PM
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: