2016-12-17 99 views
-1

我將圖像名稱存儲在數據庫中,圖像存儲在服務器上。我現在需要做的是,獲取圖像並用新名稱重命名圖像,在這種情況下,名稱與數據庫中的ID相同。從數據庫中獲取圖像,重命名服務器上的圖像並使用PHP保存數據庫中的新圖像名稱

這是我目前的代碼。

<?php 

//open the database 
include '../db/dbc.php'; 

$getimage = mysql_query("SELECT id, profile_image_url FROM users WHERE profile_image_url !='' "); 

while($image = mysql_fetch_array($getimage)) { 

    //complete image name to the original image 
    $imgdir = "../images/profile/".$image['profile_image_url']; //path where image is + image name 


    $imagename = $image['profile_image_url']; //image name old EXAMPLE: 7823jhasjda6732iojhaksd.jpg 
    $id = $image['id']; //id of the user 


    $temp = explode(".", $imagename); 
    $newfilename = $id . '.' . end($temp); // new image name with id EXAMPLE: 1023.jpg 



    move_uploaded_file($imgdir,"../images/profile/".$newfilename); 

} 


?> 

所以我設法獲取相關的圖片,並創建工作正常的新名字,但move_uploaded_file不會出於某種原因。 我現在需要做的是用$ newfile varialble中生成的新映像名稱重命名服務器上的舊映像。

任何幫助將appriciated。

Cheerz

回答

1

move_uploaded_file是移動從臨時載位置上載文件到其最終目的地。雖然您的圖片可能在某個時候上傳,但它不再是這樣。該函數預計第一個參數爲,只是一個文件名,並在該上傳文件夾中查找它。

在你的情況下,函數應該返回false,所以如果有什麼不工作,請檢查函數的結果值,並檢查what it means

您可能正在尋找rename,它從關於move_uploaded_file的頁面鏈接,並且還可以將文件重命名(移動)到不同的文件夾甚至不同的分區。

0

我用重命名是。這是我的最終解決方案。

<?php 

//open the database 
include '../db/dbc.php'; 

$getimage = mysql_query("SELECT id, profile_image_url FROM users WHERE profile_image_url !='' "); 

while($image = mysql_fetch_array($getimage)) { 

    //complete image name to the original image 
    $imgdir = "../images/profile/".$image['profile_image_url']; //path where image is + image name 


    $imagename = $image['profile_image_url']; //image name old EXAMPLE: 7823jhasjda6732iojhaksd.jpg 
    $id = $image['id']; //id of the user 


    $temp = explode(".", $imagename); 
    $newfilename = $id . '.' . end($temp); // new image name with id EXAMPLE: 1023.jpg 

    $newdir = "../images/profile/".$newfilename; 


    rename($imgdir,$newdir); 

} 

?>

相關問題