PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Hi, I'm using a form to upload a jpeg image to my server via PHP (see code). I'd like to rename the jpeg as title.jpg before uploading it to the server and also I'd like to reject the image if it isn't exactly 500x375 pixels.
I've searched the net but can only find a few tutorials about renaming files with random file names in order to avoid overwriting old ones but I actually want to overwrite old image files so I want to rename files to a specific name rather than to a random one. I can't find much either about checking an image's pixel dimensions.
<?php
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']);
$ok=1;
if ($uploaded_size > 70000)
{
echo "Your file is too large.<br>";
$ok=0;
}
if ($uploaded_type != 'image/jpeg') {
echo "You may only upload JPEG files.<br>";
$ok=0;
}
if ($ok==0)
{
Echo "Sorry your file was not uploaded";
}
else
{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ".
basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
}
?>
Last edited by Leao on Wed Aug 23, 2006 3:53 pm, edited 2 times in total.
Yup, forgive me I entered the data incorrectly. It works now.
I tried the getimagesize() function but it didn't seem to work. Even if the image was exactly 500 pixels in width I still got the 'Your file is not the correct width...' error message and also
PHP Warning: getimagesize(mountain.jpg): failed to open stream: No such file or directory in C:\hshome\stpatric\yourdomain.org\test\upload.php on line 7
list($width, $height) = getimagesize($_FILES['uploaded']['name']) ;
if ($width != 500)
{
echo "Your file is not the correct width of 500 pixels.<br>";
$ok=0;
}
list($ImportWidth,$ImportHeight,$ImageMimeType) = getimagesize($_FILES['uploaded']['tmp_name']) ;
if ($ImportWidth != 500)
{
echo "Your file is not the correct width of 500 pixels.<br>";
$ok=0;
}