2014-09-04 109 views
1

我即將開始使用PHP腳本來導入csv數據庫。從URL複製圖像,更改名稱並保存到文件夾

該csv有一個列與產品圖像的網址。

我需要做的是得到圖像,檢查它是什麼類型的文件(JPG,PNG等),更改名稱,將文件保存到服務器上的文件夾,然後插入文件名進入數據庫。

插入到數據庫的位我可以做,它的文件名與im混淆了。

是否有可能搶喜歡你的信息將上傳文件時,例如:

使用文件輸入HTML表單

$_FILES['file']['name']; 
or 
$_FILES['file']['type']; 

如果下載一個文件,上傳文件,能這是可能的

$downloaded_image['name']; 
or 
$downloaded_image['type']; 

或是完全脫離標記?

我從來沒有這樣做過,並且大多數在stackoverflow上的答案都不能完全回答我的問題,所以希望有人能指出我在正確的方向如何做到這一點。

編輯/更新:

會是這樣的工作來獲取文件屬性...

$image_id = '123456'; 
$the_image = file_get_contents($downloaded_image); 
$image_name = $the_image['name']; 
$image_type = $the_image['type']; 

$new_name = $image_id . '.' . $image_type; 

$img_path = '/images/'; 
$save_image = file_put_contents($img_path, $new_name); 
if($save_image) { 
echo 'image saved'; 
} else { 
echo 'Not Saved'; 
} 

希望IM做一些感覺。

更新:這裏是腳本,因爲它是(仍然需要整理)

define('CSV_PATH','E:/xampp/htdocs/aff/csv-import/'); 
// path where your CSV file is located 

$csv_file = CSV_PATH . "infotuts.csv"; // Name of your CSV file 
$csvfile = fopen($csv_file, 'r'); 
$theData = fgets($csvfile); 
$i = 0; 
while (!feof($csvfile)) { 
    $csv_data[] = fgets($csvfile, 1024); 
    $csv_array = explode(",", $csv_data[$i]); 
    $insert_csv = array(); 
    $insert_csv['test_id'] = $csv_array[0]; 
    // $insert_csv['test_aw_id'] = $csv_array[1]; 
    // $insert_csv['test_name'] = $csv_array[2]; 
    $image_id = $csv_array[1]; 
    $download_image = $csv_array[2]; 

    // Store the original filename 
    $original_name = basename($download_image); 

    // Original extension by string manipulation 
    $original_extension = substr($original_name, strrpos($original_name, '.')); // ".jpg" 

    // An array to match mime types from finfo_file() with extensions 
    // Use of finfo_file() is recommended if you can't trust the input 
    // filename's extension 
    $types = array('image/jpeg' => '.jpg','image/png' => '.png','image/gif' => '.gif'); 

    // Get the file and save it 
    $img = file_get_contents($download_image); 
    $stored_name = 'images/' . $image_id . $original_extension; 
    if ($img) { 
     file_put_contents($stored_name); 

     // Get the filesize if needed 
     $size = filesize($stored_name); 

     // If you don't care about validating the mime type, skip all of this... 
     // Check the file information 
     $finfo = finfo_open(FILEINFO_MIME_TYPE); 
     $mimetype = finfo_file($finfo, $stored_name); 

     // Lookup the type in your array to get the extension 
     if (isset($types[$mimetype])) { 
     // if the reported type doesn't match the original extension, rename the file 
     if ($types[$mimetype] != $original_extension) { 
      rename($stored_name, 'images/' . $image_id . $types[$mimetype]); 
     } 
     } 
     else { 
     // unknown type, handle accordingly... 
     } 
     finfo_close($finfo); 

     $query = "INSERT INTO test(test_id, test_aw_id, test_name) VALUES ('', '$image_id', '$stored_name')"; 
     $n=mysqli_query($con, $query); 
     $i++; 
    } 
    else { 
     echo 'Could not get file'; 
    } 

} 
fclose($csvfile); 
+0

這要看是什麼樣打開網址PHP配置。如果它被許可,那麼你可以使用$ fileContents = file_get_contents(image_url_there);您可以從文件擴展名中獲得的類型。否則,您應該使用curl來獲取外部內容 – bksi 2014-09-04 00:11:14

+1

否。file_get_contents獲取文件內容。文件名在您打開的網址中。您可以從文件擴展名獲得的文件類型或使用fileinfo函數。 – bksi 2014-09-04 00:38:23

+0

在你提出的代碼中,'$ download_image'從哪裏來?這是您的文件系統上的圖像,您提供給用戶下載,還是這是您從其他位置檢索(下載)並保存的圖像?我想我早點誤解了你的意圖。 – 2014-09-04 01:07:11

回答

3

通過與file_get_contents()檢索文件,你不會得到有關其格式的任何特別有用的信息。它不包含類似於$_FILES上傳的元數據。

如果圖片網址預計爲帶有擴展名的完整文件名,並且您的信任擴展名是正確的,那麼您可以將其用作您的類型。但是,finfo_file()FILEINFO_MIME_TYPE選項將探測該文件以返回其MIME類型,如image/jpegimage/png

那麼您的工作流程將是:

  1. 獲取與file_get_contents()
  2. 保存圖像到本地文件系統中的新名稱
  3. 呼叫finfo_file()檢索其MIME類型
  4. 更新您的數據庫與所需的細節。

例子:

// Assume this URL for $download_image from your CSV 
$download_image = 'http://example.com/images/img1.jpg'; 
$image_id = 12345; 

// Store the original filename 
$original_name = basename($download_image); // "img1.jpg" 
// Original extension by string manipulation 
$original_extension = substr($original_name, strrpos($original_name, '.')); // ".jpg" 

// An array to match mime types from finfo_file() with extensions 
// Use of finfo_file() is recommended if you can't trust the input 
// filename's extension 
$types = array(
    'image/jpeg' => '.jpg', 
    'image/png' => '.png', 
    'image/gif' => '.gif' 
    // Other types as needed... 
); 

// Get the file and save it 
$img = file_get_contents($download_image); 
$stored_name = 'images/' . $image_id . $original_extension; 
if ($img) { 
    file_put_contents($stored_name, $img); 

    // Get the filesize if needed 
    $size = filesize($stored_name); 

    // If you don't care about validating the mime type, skip all of this... 
    // Check the file information 
    $finfo = finfo_open(FILEINFO_MIME_TYPE); 
    $mimetype = finfo_file($finfo, $stored_name); 

    // Lookup the type in your array to get the extension 
    if (isset($types[$mimetype])) { 
    // if the reported type doesn't match the original extension, rename the file 
    if ($types[$mimetype] != $original_extension) { 
     rename($stored_name, 'images/' . $image_id . $types[$mimetype]); 
    } 
    } 
    else { 
    // unknown type, handle accordingly... 
    } 
    finfo_close($finfo); 

    // Now save all the extra info you retrieved into your database however you normally would 
    // $mimetype, $original_name, $original_extension, $filesize 
} 
else { 
    // Error, couldn't get file 
} 

如果你想從你已經有了擴展的MIME類型的字符串,並且不確認與FINFO類型,你可以翻轉$types交換與值的鍵。

if (in_array($original_extension), $types) { 
    $mimetype = array_flip($types)[$original_extension]; 
} 
+0

是的,它剛剛失去了凌晨3點,我即將準備好爲我的牀準備4個小時的睡眠,然後孩子們就把我拖下牀了。早上。大聲笑......邁克爾,你已經超越了。我只是在尋找指針,但是我得到了幾乎所有我需要開始的那部分腳本。真的很感謝幫助。非常感謝! – 2014-09-04 02:13:04

+0

很高興幫助,祝你好運。 (我的孩子也在5:30起牀) – 2014-09-04 02:14:33

+0

嘿@MichaelBerkowski,我嘗試了上面的內容,對套件測試腳本進行了一些更改。我得到了以下錯誤:Warning:file_get_contents(http://www.partydomain .co.uk/images/P/21478.jpg):無法打開流:HTTP請求失敗! HTTP/1.0 404在第34行的E:\ xampp \ htdocs \ aff \ admin \ import_products.php中找不到第34行 - 第34行是'$ img = file_get_contents($ download_image);' – 2014-09-04 15:26:33

0
<?php 

include_once('includes/functions.php'); 

define('CSV_PATH','E:/xampp/htdocs/aff/csv-import/'); 

$csv_file = CSV_PATH . "infotuts.csv"; 
$csvfile = fopen($csv_file, 'r'); 
$theData = fgets($csvfile); 
$i = 0; 
while (!feof($csvfile)) { 
    $csv_data[] = fgets($csvfile, 1024); 
    $csv_array = explode(",", $csv_data[$i]); 
    $insert_csv = array(); 
    $insert_csv['test_id'] = $csv_array[0]; 
    $insert_csv['test_aw_id'] = $csv_array[1]; 
    $insert_csv['test_name'] = $csv_array[2]; 

    $image_id = $insert_csv['test_aw_id']; 
    $download_image = $insert_csv['test_name']; 

    $original_name = basename($download_image); 

    $original_extension = substr($original_name, strrpos($original_name, '.')); // ".jpg" 

    $types = array('image/jpeg' => '.jpg','image/png' => '.png','image/gif' => '.gif'); 

    $img = file_get_contents($download_image); 
    $stored_name = $image_id . $original_extension; 
    $stored_name = trim($stored_name); 
    if ($img) { 
     file_put_contents($stored_name, $img); 

     //$size = filesize($stored_name); 

     $finfo = finfo_open(FILEINFO_MIME_TYPE); 
     $mimetype = finfo_file($finfo, $stored_name); 
     if (isset($types[$mimetype])) { 
     if ($types[$mimetype] != $original_extension) { 
      rename($stored_name, 'E:/xampp/htdocs/aff/images/products/' . $stored_name); 

     } 
     } 
     else { 

     } 
     finfo_close($finfo); 

     $query = "INSERT INTO test(test_id, test_aw_id, test_name) VALUES ('', '$image_id', '$stored_name')"; 
     $n=mysqli_query($con, $query); 
     $i++; 
    } 
    else { 
     echo 'Could not get file'; 
    } 

} 
fclose($csvfile); 

echo "File data successfully imported to database!!"; 
mysqli_close($con); 
?> 
相關問題