2016-12-17 98 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); 

} 

?>

相关问题