2016-03-10 74 views
0

因此,我想在將文件名壓縮到數據庫和分配的文件夾之前,將一個隨機數和一個_添加到原始文件名的乞求中。PHP在上傳前重命名圖像文件名

我一直都晚上四處尋找互聯網和不能找到我的解決方案

繼承人我的代碼

這完全適用於我的文件上傳到數據庫和文件夾,但如果你把上的照片ios設備文件始終命名爲image.jpg

$images_arr = array(); 

$target_dir = "uploads/"; 
$target = $target_dir.$_FILES['photo']['name']; 

    //$target = $target . basename($_FILES['photo']['name']); 

    //This gets all the other information from the form 
$userId=$_POST['userId']; 
$name=$_POST['nameMember']; 
$bandMember=$_POST['bandMember']; 
$pic=($_FILES['photo']['name']); 
$about=$_POST['aboutMember']; 


    // Connects to your Database 
mysql_connect("", "", "") or die(mysql_error()) ; 
mysql_select_db("") or die(mysql_error()) ; 

    //Writes the information to the database 
mysql_query("INSERT INTO portal_phocagallery (user_id,title,alias,filename,description) 
VALUES ('$userId', '$name', '$bandMember', '$pic', '$about')") ; 

//Writes the photo to the server 
if(move_uploaded_file($_FILES ['photo']['tmp_name'], $target)){ 

     //Tells you if its all ok 
    echo "The file ". $target_dir.$_FILES['photo']['name']. " has been uploaded, and your information has been added to the directory"; 
    $images_arr[] = $target; 
} 
else { 

    //Gives and error if its not 
    echo "Sorry, there was a problem uploading your file."; 
} 


function errorMessage($str) { 
return '<div style="width:50%; margin:0 auto; border:2px solid #F00;padding:2px; color:#000; margin-top:10px; text-align:center;">' . $str . '</div>'; 
} 

function successMessage($str) { 
return '<div style="width:50%; margin:0 auto; border:2px solid #06C;padding:2px; color:#000; margin-top:10px; text-align:center;">' . $str . '</div>'; 
} 

感謝提前!

我得到了它的代碼的最上面幾行更改爲此

$images_arr = array(); 
      //This is the directory where images will be saved 
     $target_dir = "uploads/"; 
     $file = rand().'_'.$_FILES['photo']['name']; 
     $target = $target_dir.$file; 

      //$target = $target . basename($_FILES['photo']['name']); 

      //This gets all the other information from the form 
     $userId=$_POST['userId']; 
     $name=$_POST['nameMember']; 
     $bandMember=$_POST['bandMember']; 
     $pic=$file; 
     $about=$_POST['aboutMember']; 
+0

不可能。當文件上傳後,你可以隨心所欲地做任何事情。它是$ _FILES ['photo'] ['temp'],它是實際的文件。詳見手冊http://php.net/manual/en/features.file-upload.php – jeff

+1

'$ pic = rand()。'_'。$ _ FILES ['photo'] ['name'];'' –

+1

但是,然後$ target應該是'= $ target_dir。$ pic;' – VolkerK

回答

0

重命名上傳文件的完美的工作,從臨時文件夾移動時,通過$ NEW_TARGET變量,而不是$的目標

$images_arr = array(); 

$target_dir = "uploads/"; 
$target = $target_dir.$_FILES['photo']['name']; 
$pic = rand()."_".$_FILES['photo']['name']; 
$new_target = $target_dir.$pic; 

//$target = $target . basename($_FILES['photo']['name']); 

//This gets all the other information from the form 
$userId=$_POST['userId']; 
$name=$_POST['nameMember']; 
$bandMember=$_POST['bandMember']; 
//$pic=($_FILES['photo']['name']); 
$about=$_POST['aboutMember']; 


// Connects to your Database 
mysql_connect("", "", "") or die(mysql_error()) ; 
mysql_select_db("") or die(mysql_error()) ; 

//Writes the information to the database 
mysql_query("INSERT INTO portal_phocagallery (user_id,title,alias,filename,description) 
VALUES ('$userId', '$name', '$bandMember', '$pic', '$about')") ; 

//Writes the photo to the server 
if(move_uploaded_file($_FILES ['photo']['tmp_name'], $new_target)){ 

    //Tells you if its all ok 
    echo "The file ". $new_target. " has been uploaded, and your information has been added to the directory"; 
    $images_arr[] = $new_target; 
} else { 
    //Gives and error if its not 
    echo "Sorry, there was a problem uploading your file."; 
} 


function errorMessage($str) { 
    return '<div style="width:50%; margin:0 auto; border:2px solid #F00;padding:2px; color:#000; margin-top:10px; text-align:center;">' . $str . '</div>'; 
} 

function successMessage($str) { 
    return '<div style="width:50%; margin:0 auto; border:2px solid #06C;padding:2px; color:#000; margin-top:10px; text-align:center;">' . $str . '</div>'; 
}