http://www.cnblogs.com/zjfree/archive/2011/01/06/1929100.html
上面地址中,在內容說明中有個PHP上傳代碼,這個PHP代碼要怎麼取得原檔案名稱?
PHP代碼:
$file_name = date('Ymd') . '_' . time() . '_' . mt_rand(1, 1000) . '.jpg';
$file_path = '/img/user_img/' . date('Y_m') . '/';
$dir = dirname(__FILE__) . $file_path;
//建立目錄失敗
@mkdir($dir, 0777, true);
$img_path = $dir . $file_name;
$img_url = 'http://' . $_SERVER["HTTP_HOST"] . $file_path . $file_name;
if (count($_FILES) > 0)
{
$f = current($_FILES);
move_uploaded_file($f["tmp_name"], $img_path);
}
else
{
$rawpostdata = file_get_contents("php://input");
$myfile = fopen($img_path, "w") or die("Unable to open file!");
fwrite($myfile, $rawpostdata);
fclose($myfile);
}
echo $img_url;
?>
回複討論(解決方案)
$rawpostdata = file_get_contents("php://input"); //讀取傳入的流
$myfile = fopen($img_path, "w") or die("Unable to open file!");
fwrite($myfile, $rawpostdata); //將得到的資料寫入檔案
fclose($myfile);
顯然他並沒有傳遞檔案名稱,自然你也就得不到了
但如果視奏這個分支的
if (count($_FILES) > 0)
{
$f = current($_FILES);
move_uploaded_file($f["tmp_name"], $img_path);
}
$f["name"] 就是原始檔案名
$rawpostdata = file_get_contents("php://input"); //讀取傳入的流
$myfile = fopen($img_path, "w") or die("Unable to open file!");
fwrite($myfile, $rawpostdata); //將得到的資料寫入檔案
fclose($myfile);
顯然他並沒有傳遞檔案名稱,自然你也就得不到了
但如果視奏這個分支的
if (count($_FILES) > 0)
{
$f = current($_FILES);
move_uploaded_file($f["tmp_name"], $img_path);
}
$f["name"] 就是原始檔案名
謝謝解釋,看明白了,讀取傳入的流 方式時只有圖片流資訊,沒有檔案名稱資訊