Forum Moderators: coopster
I also have a software that can take a large image and create a thumbnail that I can then upload with the big image.
I was wondering if there would be a php script that can not only uploads the large image but also creates a thumbnail out of it during the upload. Or am I just dreaming here :-)
Thanks,
Richard
Image upload and Thumbnail Generator [webmasterworld.com] msg#1296727
I went to the post and copied the code there but I ran into this error msg.
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in .../loademup.php on line 30
Which is this line of code here...
print "<strong>$_FILES['userfile']['name'][$q]</strong> did not upload!";
Any ideas where the bug is?
Richard
brace/semicolon/parentheses
not sure, the code worked for me long ago, though I now use something that is far from the original and different every time
check to see everything matches
you could try that line like this too
print "<strong>" . $_FILES['userfile']['name'][$q] . "</strong> did not upload!";
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in ../loademup.php on line 21
which is this line
while($i = mysql_fetch_array($result)){
Then I have these 2 dir related error msgs here..
Warning: move_uploaded_file(/images/): failed to open stream: Permission denied in ../loademup.php on line 26
Warning: move_uploaded_file(): Unable to move '/tmp/phpyLWNxa' to '/images/' in ../loademup.php on line 26
59448201.jpg did not upload!
I created the images and tmp directories and CHMOD them to 755 each and still can seem to get the script to upload my image.
As if that wasn't wnough it pretty much rejects all the functions in the script...
Warning: getimagesize(/images/): failed to open stream (line 35)
Warning: imagecreatefromjpeg(/images/): (line 49)
Warning: imagecopyresized(): supplied argument is not a valid Image resource (line 51)
Warning: imagejpeg(): Unable to open '/images/thumbs/' for writing (line 52)
With all those errors i'm wonderng if it's not my server that's going nuts or the script here.
Any advices on where to begin debugging here?
Sorry for all the trouble.
Richard
Most of these errors are related. To prevent some of them appearing you should check to make sure the file exists before attempting any processing. Try the file_exists [uk2.php.net] function.
The permissions error is because the directory you are attempting to write to does not have write permissions. Use 777 instead.
The problem with your database will be the actual query and not the line you posted. Your query is failing somewhere which is causing the error. Debugging would be to use mysql_error() in your query line. Example:
$result = mysql_query($query) or die(mysql_error());
dc
I did the changes you asked me to do and almost all the error msgs stopped (so far so good) except that now it says this...
"Query was empty"
If the permissions are set to 777, how come the query was empty? The username, password and db name that I have setup worked with other php files so the db should not be the problem here.
Also, what's the use of file_exist.php if I can't upload 1 single file! I have my FTP open and can see if/when a file will finally be uploaded in my dirs.
Below is the loademup.php code I have for now. I'm close but it needs a little twist somewhere..any ideas?
Richard
= = = = = = =
<html>
<head><title>Enter Name and Descriptions of Pics</title></head>
<body>
<h1>Enter Name and Descriptions of Pics</h1>
<form action="view.php" method="post">
<?php
$dbh=mysql_connect("localhost", "user", "pw") or die ('I cannot connect to the database.');
mysql_select_db("my_db");
$uploaddir = '/images/';
$tot = count($userfile);
$num = 0;
for($q=0;$q<$tot;$q++){
if ($_FILES['userfile']['name'][$q] == "") continue;
$num = $num + 1;
$sql = "SELECT pic_id FROM pics ORDER BY pic_id DESC LIMIT 1";
$result = mysql_query($sql);
$result = mysql_query($query) or die(mysql_error());
$new_id = $i['pic_id'] + 1;
$new_pic = "$new_id.jpg";
}
if (move_uploaded_file($_FILES['userfile']['tmp_name'][$q], $uploaddir . $new_pic)) {
$sql = "INSERT INTO `pics` ( `pic_id` , `name` , `descrip` )VALUES ('$new_id', NULL , NULL);";
mysql_query($sql);
} else {
print "<strong>" . $_FILES['userfile']['name'][$q] . "</strong> did not upload!";
}
$new_thumb = "thumbs/$new_pic";
$sourcefile = "$uploaddir$new_pic";
$picsize = getimagesize("$sourcefile");
$source_x = $picsize[0];
$source_y = $picsize;
if ($source_x > $source_y){
$dest_x = 200;
$dest_y = 150;
} else {
$dest_x = 150;
$dest_y = 200;
}
$targetfile = "$uploaddir$new_thumb";
$jpegqual = 75;
$source_id = imagecreatefromjpeg("$sourcefile");
$target_id = imagecreate($dest_x, $dest_y);
$target_pic = imagecopyresized($target_id,$source_id,0,0,0,0,$dest_x,$dest_y,$source_x,$source_y);
imagejpeg($target_id,"$targetfile",$jpegqual);
?>
<div style="clear: both;">
<a href="/images/<?=$new_pic?>">
<img src="/images/<?=$new_thumb?>" style="float: left" />
</a>
<strong><?=$_FILES['userfile']['name'][$q]?></strong><br /><br />
<strong>Name:</strong><br />
<input type="text" name="pic_name[<?=$num?>]" /><br /><br />
<strong>Description:</strong><br />
<textarea name="descrip[<?=$num?>]"></textarea>
<input type="hidden" name="pic_id[<?=$num?>]" value="<?=$new_id?>" />
</div>
<?php
?>
<input type="submit" value="Click to Save Descriptions" />
</form>
</body>
</html>
$result = mysql_query($sql);$result = mysql_query($query) or die(mysql_error());
You only need the query line once. The second query is using the $query variable, which doesn`t exist. Hence the empty query error. Also, with file_exists, what I was getting at is you should check that the file is in place before doing anything with it. For example, your getimagesize error is appearing because the file does not exist.
So, after uploading, check the file has actually been uploaded. If it has, continue.
dc
The "Query was empty" error msg is gone now but now I have these 4 error msgs related that came back related to the missing uploaded file.
I have pasted the error messages below with their specific lines of code for you to help me out figuring where the bugs are.
Again, the images and thumbs dir under it have both the 777 permissions so I don't understand why the file didn't upload!
If you have any clue please let me know.
Thanks again for your time,
Richard
= = = = = = = =
did not upload!
Warning: getimagesize(/images/1.jpg): failed to open stream: No such file or directory in ../loademup.php on line 34
which is this line here...
$picsize = getimagesize("$sourcefile");
Warning: imagecreatefromjpeg(/images/1.jpg): failed to open stream: No such file or directory in ../loademup.php on line 48
which is this line here...
$source_id = imagecreatefromjpeg("$sourcefile");
Warning: imagecopyresized(): supplied argument is not a valid Image resource in ../loademup.php on line 50
which is this line here...
$target_pic = imagecopyresized($target_id,$source_id,0,0,0,0,$dest_x,$dest_y,$source_x,$source_y);
and finally this error msg
Warning: imagejpeg(): Unable to open '/images/thumbs/1.jpg' for writing in ../loademup.php on line 51
which is this line here...
imagejpeg($target_id,"$targetfile",$jpegqual);