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"] 就是原始文件名
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号