多檔案上傳[Flash]外掛程式,PHP版的一個問題

來源:互聯網
上載者:User
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"] 就是原始檔案名



謝謝解釋,看明白了,讀取傳入的流 方式時只有圖片流資訊,沒有檔案名稱資訊
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.