Как да модна този код да преименува файла след качване

wizzyto

Registered
Здравейте !
Имам това малко скриптче за качване на снимки и преоразмеряване, но искам също и да ги преименува на произволно число (прим. rand(0000,9999)), но като се опитах с rand-а ми изкара, че очаква T_VARIABLE (демек да не е число ;D). Някой може ли да ми го направи да го преименува на произволно число или име?

PHP:
<?php ini_set("memory_limit", "200000000"); // for large images so that we do not get "Allowed memory exhausted"?>
<?php
		
// upload the file
if ((isset($_POST["submitted_form"])) && ($_POST["submitted_form"] == "image_upload_form")) {
	
	// file needs to be jpg,gif,bmp,x-png and 4 MB max
	if (($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg" || $_FILES["image_upload_box"]["type"] == "image/gif" || $_FILES["image_upload_box"]["type"] == "image/x-png") && ($_FILES["image_upload_box"]["size"] < 4000000))
	{
		
  
		// some settings
		$max_upload_width = 4000;
		$max_upload_height = 4000;
		  
		// if user chosed properly then scale down the image according to user preferances
		if(isset($_REQUEST['max_width_box']) and $_REQUEST['max_width_box']!='' and $_REQUEST['max_width_box']<=$max_upload_width){
			$max_upload_width = $_REQUEST['max_width_box'];
		}    
		if(isset($_REQUEST['max_height_box']) and $_REQUEST['max_height_box']!='' and $_REQUEST['max_height_box']<=$max_upload_height){
			$max_upload_height = $_REQUEST['max_height_box'];
		}	

		
		// if uploaded image was JPG/JPEG
		if($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg"){	
			$image_source = imagecreatefromjpeg($_FILES["image_upload_box"]["tmp_name"]);
		}		
		// if uploaded image was GIF
		if($_FILES["image_upload_box"]["type"] == "image/gif"){	
			$image_source = imagecreatefromgif($_FILES["image_upload_box"]["tmp_name"]);
		}	
		// BMP doesn't seem to be supported so remove it form above image type test (reject bmps)	
		// if uploaded image was BMP
		if($_FILES["image_upload_box"]["type"] == "image/bmp"){	
			$image_source = imagecreatefromwbmp($_FILES["image_upload_box"]["tmp_name"]);
		}			
		// if uploaded image was PNG
		if($_FILES["image_upload_box"]["type"] == "image/x-png"){
			$image_source = imagecreatefrompng($_FILES["image_upload_box"]["tmp_name"]);
		}
		
		$remote_file = "../gl/".$_FILES["image_upload_box"]['name'];
		imagejpeg($image_source,$remote_file,100);
		chmod($remote_file,0644);
	

		// get width and height of original image
		list($image_width, $image_height) = getimagesize($remote_file);
	
		if($image_width>$max_upload_width || $image_height >$max_upload_height){
			$proportions = $image_width/$image_height;
			
			if($image_width>$image_height){
				$new_width = $max_upload_width;
				$new_height = round($max_upload_width/$proportions);
			}		
			else{
				$new_height = $max_upload_height;
				$new_width = round($max_upload_height*$proportions);
			}		
			
			
			$new_image = imagecreatetruecolor($new_width , $new_height);
			$image_source = imagecreatefromjpeg($remote_file);

			imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
			
			imagejpeg($new_image,$remotefile,100);
			
			imagedestroy($new_image);
		}
		
		imagedestroy($image_source);
		
		
		header("Location: submit.php?upload_message=Успешно качена в галерията.&upload_message_type=success&show_image=".$_FILES["image_upload_box"]["name"]);
		exit;
	}
	else{
		header("Location: submit.php?upload_message=make sure the file is jpg, gif or png and that is smaller than 4MB&upload_message_type=error");
		exit;
	}
}
?>
 
PHP:
<?php ini_set("memory_limit", "200000000"); // for large images so that we do not get "Allowed memory exhausted"?>
<?php
		
// upload the file
if ((isset($_POST["submitted_form"])) && ($_POST["submitted_form"] == "image_upload_form")) {
	
	// file needs to be jpg,gif,bmp,x-png and 4 MB max
	if (($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg" || $_FILES["image_upload_box"]["type"] == "image/gif" || $_FILES["image_upload_box"]["type"] == "image/x-png") && ($_FILES["image_upload_box"]["size"] < 4000000))
	{
		
  
		// some settings
		$max_upload_width = 4000;
		$max_upload_height = 4000;
		  
		// if user chosed properly then scale down the image according to user preferances
		if(isset($_REQUEST['max_width_box']) and $_REQUEST['max_width_box']!='' and $_REQUEST['max_width_box']<=$max_upload_width){
			$max_upload_width = $_REQUEST['max_width_box'];
		}    
		if(isset($_REQUEST['max_height_box']) and $_REQUEST['max_height_box']!='' and $_REQUEST['max_height_box']<=$max_upload_height){
			$max_upload_height = $_REQUEST['max_height_box'];
		}	

		
		// if uploaded image was JPG/JPEG
		if($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg"){	
			$image_source = imagecreatefromjpeg($_FILES["image_upload_box"]["tmp_name"]);
		}		
		// if uploaded image was GIF
		if($_FILES["image_upload_box"]["type"] == "image/gif"){	
			$image_source = imagecreatefromgif($_FILES["image_upload_box"]["tmp_name"]);
		}	
		// BMP doesn't seem to be supported so remove it form above image type test (reject bmps)	
		// if uploaded image was BMP
		if($_FILES["image_upload_box"]["type"] == "image/bmp"){	
			$image_source = imagecreatefromwbmp($_FILES["image_upload_box"]["tmp_name"]);
		}			
		// if uploaded image was PNG
		if($_FILES["image_upload_box"]["type"] == "image/x-png"){
			$image_source = imagecreatefrompng($_FILES["image_upload_box"]["tmp_name"]);
		}
		$random_name=rand(0000,9999);
$random_named=rand(0000,9999);
$random_namea=rand(0000,9999);
		$remote_file = "../gl/".$random_name.$random_named.$random_namea;
		imagejpeg($image_source,$remote_file,100);
		chmod($remote_file,0644);
	

		// get width and height of original image
		list($image_width, $image_height) = getimagesize($remote_file);
	
		if($image_width>$max_upload_width || $image_height >$max_upload_height){
			$proportions = $image_width/$image_height;
			
			if($image_width>$image_height){
				$new_width = $max_upload_width;
				$new_height = round($max_upload_width/$proportions);
			}		
			else{
				$new_height = $max_upload_height;
				$new_width = round($max_upload_height*$proportions);
			}		
			
			
			$new_image = imagecreatetruecolor($new_width , $new_height);
			$image_source = imagecreatefromjpeg($remote_file);

			imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
			
			imagejpeg($new_image,$remotefile,100);
			
			imagedestroy($new_image);
		}
		
		imagedestroy($image_source);
		
		
		header("Location: submit.php?upload_message=Успешно качена в галерията.&upload_message_type=success&show_image=".$_FILES["image_upload_box"]["name"]);
		exit;
	}
	else{
		header("Location: submit.php?upload_message=make sure the file is jpg, gif or png and that is smaller than 4MB&upload_message_type=error");
		exit;
	}
}
?>
 
Трябва ми да замества изцяло името на файла, а не да слага номер пред него. Общо взето проблема е, че когато се качват снимки с име на български галерията не ги отчита и изкарва грешки.
 
Дойде ми и друго на акъла.

PHP:
<?php ini_set("memory_limit", "200000000"); // for large images so that we do not get "Allowed memory exhausted"?>
<?php
                
// upload the file
if ((isset($_POST["submitted_form"])) && ($_POST["submitted_form"] == "image_upload_form")) {
        
        // file needs to be jpg,gif,bmp,x-png and 4 MB max
        if (($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg" || $_FILES["image_upload_box"]["type"] == "image/gif" || $_FILES["image_upload_box"]["type"] == "image/x-png") && ($_FILES["image_upload_box"]["size"] < 4000000))
        {
                
  
                // some settings
                $max_upload_width = 4000;
                $max_upload_height = 4000;
                  
                // if user chosed properly then scale down the image according to user preferances
                if(isset($_REQUEST['max_width_box']) and $_REQUEST['max_width_box']!='' and $_REQUEST['max_width_box']<=$max_upload_width){
                        $max_upload_width = $_REQUEST['max_width_box'];
                }    
                if(isset($_REQUEST['max_height_box']) and $_REQUEST['max_height_box']!='' and $_REQUEST['max_height_box']<=$max_upload_height){
                        $max_upload_height = $_REQUEST['max_height_box'];
                }       

                
                // if uploaded image was JPG/JPEG
                if($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg"){        
                        $image_source = imagecreatefromjpeg($_FILES["image_upload_box"]["tmp_name"]);
                }               
                // if uploaded image was GIF
                if($_FILES["image_upload_box"]["type"] == "image/gif"){ 
                        $image_source = imagecreatefromgif($_FILES["image_upload_box"]["tmp_name"]);
                }       
                // BMP doesn't seem to be supported so remove it form above image type test (reject bmps)       
                // if uploaded image was BMP
                if($_FILES["image_upload_box"]["type"] == "image/bmp"){ 
                        $image_source = imagecreatefromwbmp($_FILES["image_upload_box"]["tmp_name"]);
                }                       
                // if uploaded image was PNG
                if($_FILES["image_upload_box"]["type"] == "image/x-png"){
                        $image_source = imagecreatefrompng($_FILES["image_upload_box"]["tmp_name"]);
                }
function getExtension($str) {

         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
 }
$filename = stripslashes($_FILES["image_upload_box"]['name']);
        $extension = getExtension($filename);
  $extension = strtolower($extension);
                $random_name=rand(0000,9999);
                $remote_file = "../gl/".$random_name.md5($_FILES["image_upload_box"]['name']).$extension;
                imagejpeg($image_source,$remote_file,100);
                chmod($remote_file,0644);
        

                // get width and height of original image
                list($image_width, $image_height) = getimagesize($remote_file);
        
                if($image_width>$max_upload_width || $image_height >$max_upload_height){
                        $proportions = $image_width/$image_height;
                        
                        if($image_width>$image_height){
                                $new_width = $max_upload_width;
                                $new_height = round($max_upload_width/$proportions);
                        }               
                        else{
                                $new_height = $max_upload_height;
                                $new_width = round($max_upload_height*$proportions);
                        }               
                        
                        
                        $new_image = imagecreatetruecolor($new_width , $new_height);
                        $image_source = imagecreatefromjpeg($remote_file);

                        imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
                        
                        imagejpeg($new_image,$remotefile,100);
                        
                        imagedestroy($new_image);
                }
                
                imagedestroy($image_source);
                
                
                header("Location: submit.php?upload_message=Успешно качена в галерията.&upload_message_type=success&show_image=".$_FILES["image_upload_box"]["name"]);
                exit;
        }
        else{
                header("Location: submit.php?upload_message=make sure the file is jpg, gif or png and that is smaller than 4MB&upload_message_type=error");
                exit;
        }
}
?>
 
Без да го пробвам ще ти кажа, че не работи - няма разширение.
md5-a кодира дори и .jpg -то, което е накрая на името ;D
 
А, да.


PHP:
<?php ini_set("memory_limit", "200000000"); // for large images so that we do not get "Allowed memory exhausted"?>
<?php
                
// upload the file
if ((isset($_POST["submitted_form"])) && ($_POST["submitted_form"] == "image_upload_form")) {
        
        // file needs to be jpg,gif,bmp,x-png and 4 MB max
        if (($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg" || $_FILES["image_upload_box"]["type"] == "image/gif" || $_FILES["image_upload_box"]["type"] == "image/x-png") && ($_FILES["image_upload_box"]["size"] < 4000000))
        {
                
  
                // some settings
                $max_upload_width = 4000;
                $max_upload_height = 4000;
                  
                // if user chosed properly then scale down the image according to user preferances
                if(isset($_REQUEST['max_width_box']) and $_REQUEST['max_width_box']!='' and $_REQUEST['max_width_box']<=$max_upload_width){
                        $max_upload_width = $_REQUEST['max_width_box'];
                }    
                if(isset($_REQUEST['max_height_box']) and $_REQUEST['max_height_box']!='' and $_REQUEST['max_height_box']<=$max_upload_height){
                        $max_upload_height = $_REQUEST['max_height_box'];
                }       

                
                // if uploaded image was JPG/JPEG
                if($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg"){        
                        $image_source = imagecreatefromjpeg($_FILES["image_upload_box"]["tmp_name"]);
                }               
                // if uploaded image was GIF
                if($_FILES["image_upload_box"]["type"] == "image/gif"){ 
                        $image_source = imagecreatefromgif($_FILES["image_upload_box"]["tmp_name"]);
                }       
                // BMP doesn't seem to be supported so remove it form above image type test (reject bmps)       
                // if uploaded image was BMP
                if($_FILES["image_upload_box"]["type"] == "image/bmp"){ 
                        $image_source = imagecreatefromwbmp($_FILES["image_upload_box"]["tmp_name"]);
                }                       
                // if uploaded image was PNG
                if($_FILES["image_upload_box"]["type"] == "image/x-png"){
                        $image_source = imagecreatefrompng($_FILES["image_upload_box"]["tmp_name"]);
                }
function getExtension($str) {

         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
 }
$filename = stripslashes($_FILES["image_upload_box"]['name']);
        $extension = getExtension($filename);
  $extension = strtolower($extension);
                $random_name=rand(0000,9999);
                $remote_file = "../gl/".$random_name.md5($_FILES["image_upload_box"]['name']).".".$extension;
                imagejpeg($image_source,$remote_file,100);
                chmod($remote_file,0644);
        

                // get width and height of original image
                list($image_width, $image_height) = getimagesize($remote_file);
        
                if($image_width>$max_upload_width || $image_height >$max_upload_height){
                        $proportions = $image_width/$image_height;
                        
                        if($image_width>$image_height){
                                $new_width = $max_upload_width;
                                $new_height = round($max_upload_width/$proportions);
                        }               
                        else{
                                $new_height = $max_upload_height;
                                $new_width = round($max_upload_height*$proportions);
                        }               
                        
                        
                        $new_image = imagecreatetruecolor($new_width , $new_height);
                        $image_source = imagecreatefromjpeg($remote_file);

                        imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
                        
                        imagejpeg($new_image,$remotefile,100);
                        
                        imagedestroy($new_image);
                }
                
                imagedestroy($image_source);
                
                
                header("Location: submit.php?upload_message=Успешно качена в галерията.&upload_message_type=success&show_image=".$_FILES["image_upload_box"]["name"]);
                exit;
        }
        else{
                header("Location: submit.php?upload_message=make sure the file is jpg, gif or png and that is smaller than 4MB&upload_message_type=error");
                exit;
        }
}
?>
 

Back
Горе