I have a script that runs to upload an image, then resize it into two different images, one is a 540px wide image and the other is a 154px wide image. The script has been running fine for me until i go to rename the files!
i would like to completely rename the file to be a random number followed by the original extension.
i have the random number followed by a . in the variable $ran2 and the original file extention in the variable $ext – i believe i have anyway!
it was working until i wanted to add the rename in towards the bottom of the script!
i believe it is this part that is going wrong: <– i havent got the file extension in a variable!
$filename = "uploads/original/". $ran2.$ext;
$filename1 = "uploads/thumbs/". $ran2.$ext;
but i havent got the foggiest idea what to change it to to make it work!
<?php
error_reporting(0);
$change="";
$abc="";
define ("MAX_SIZE","800");
$ran = rand () ;
$ran2 = $ran.".";
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$errors=0;
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$image =$_FILES["file"]["name"];
$uploadedfile = $_FILES['file']['tmp_name'];
if ($image)
{
$filename = stripslashes($_FILES['file']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
echo 'Unknown Image extension';
$errors=1;
}
else
{
$size=filesize($_FILES['file']['tmp_name']);
if ($size > MAX_SIZE*1024)
{
echo 'You have exceeded the size limit!';
$errors=1;
}
if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
}
else if($extension=="png")
{
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
}
else
{
$src = imagecreatefromgif($uploadedfile);
}
echo $scr;
list($width,$height)=getimagesize($uploadedfile);
$newwidth=520;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
$newwidth1=154;
$newheight1=($height/$width)*$newwidth1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);
$filename = "uploads/original/". $ran2.$ext;
$filename1 = "uploads/thumbs/". $ran2.$ext;
imagejpeg($tmp,$filename,100);
imagejpeg($tmp1,$filename1,100);
imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
}}
}
//If no errors registred, print the success message
if(isset($_POST['Submit']) && !$errors)
{
echo 'Image Uploaded Successfully!';
}
?>
$extis undefined. You have defined$extensionbot not$extSo, use: