2016-05-12 117 views
0

我遇到以下問題。我寫了這個代碼:將圖像複製到另一個文件夾並重命名

$imagesDir = '../gender/male/'; 
$imagesDir2 = '../uploads/'; 
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE); 
$randomImage = $images[array_rand($images)]; 
$profile_picture = $randomImage; 

使用查詢後,完整的路徑被插入到數據庫表。

查詢之後,我寫了這個代碼:

$select = $db->query("SELECT * FROM users WHERE email='".$email."'"); 
$select = $select->fetch_object(); 

if(!empty($select->profile_picture)){ 
    chmod($randomImage, 0777); 
    chmod($imagesDir2, 0777); 
    $new_name = pathinfo($randomImage); 
    copy($randomImage,$imagesDir2.$new_name['basename']); 
    $new_file = md5(mt_rand()).$new_name['basename']; 
    chmod ($imagesDir2.$new_name['basename'], 0777); 
    rename($imagesDir2.$new_name['basename'],$imagesDir2.$new_file); 
    chmod('../gender/male/' . $new_name['basename'], 0777); 
    unlink('../gender/male/' . $new_name['basename']); 
    $db->query("UPDATE users SET profile_picture='".$new_file."' WHERE email='".$email."'"); 
} 

我只是無法得到它的工作。我想要它做的,

  1. 複製圖像的上傳文件夾
  2. 通過使用MD5(mt_rand())圖像名稱前(重命名上傳文件夾中的圖像如MD5(mt_rand() )1.JPG)
  3. 更新這個數據庫
  4. 取消鏈接/銷燬這是在「../gender/male/」文件夾

只有不起作用的東西是原來的取消關聯。文件仍在文件夾中。

我希望有人能幫助我。對於所有善於幫助的人,我想事先感謝你。

回答

0

首先,我想感謝自己的所有幫助。其次,對於所有未解決相似問題的讀者而言。我將if語句更改爲:

if(!empty($select->profile_picture)){ 
    chmod($imagesDir2, 0777); 
    $new_name = pathinfo($randomImage); 
    copy($randomImage,$imagesDir2.$new_name['basename']); 
    $new_file = md5(mt_rand()).$new_name['basename']; 
    chmod ($imagesDir2.$new_name['basename'], 0777); 
    rename($imagesDir2.$new_name['basename'],$imagesDir2.$new_file); 
    chmod($imagesDir.$new_name['basename'], 0777); 
    unlink($imagesDir.$new_name['basename']); 
    $db->query("UPDATE users SET profile_picture='".$new_file."' WHERE email='".$email."'"); 
} 

它的工作原理。確保您擁有正確的權限設置。我希望它能幫助你解決你的問題。

相關問題