我的目的是将从我的电脑创建的远程文件上传到特定文件夹,但我不知道下面的代码有什么问题。它上传带有名称和.jpg扩展名的文件,但不会将文件移动到指定的文件夹。
if(isset($_POST["image"])){
define("SITE_NAME","project_name/"); //constant for project name
define("SITE_PATH",$_SERVER['DOCUMENT_ROOT']."/".SITE_NAME); //constant for project base directory
define("IMAGES_URL",SITE_URL."images/"); //constant for image directory
$upload_base_dir=IMAGES_URL;
$upload_time_dir=date('Y')."/".date('m')."/".date('d')."/"; // setup directory name
$upload_dir = $upload_base_dir.$upload_time_dir;
if (!file_exists($upload_dir)) {
mkdir($upload_dir, 0777, true); //create directory if not exist
}
$input = $_POST["image"];
$file = fopen(time()."image.jpg", 'wb');
fwrite($file, $input);
//$image_name=basename($_FILES['image']['name']);
$image=time().'_'.$image_name;
move_uploaded_file($file,$upload_dir.$image);
fclose($file);
}
有什么建议吗?提前谢谢。
答案 0 :(得分:0)
move_uploaded_file($ file,$ upload_dir。$ image)仅适用于temp中的项目,可通过$ _FILES superglobal访问。如果您要将文件作为帖子发送到帖子中,那就无法使用。
1)如果文件是表单上传,请确保表单是多部分并通过$ _FILES superglobal访问您的文件
move_uploaded_file($_FILES['userfile']['tmp_name'], $yourDirectory.$yourFilename);
2)如果您通过邮寄将文件作为流发布(请记住,这只适用于小文件,因为大文件将超过请求限制)。使用fopen将文件直接保存到它的destiantion,或者在创建后使用rename()移动它: - http://php.net/manual/en/function.rename.php
rename($currentFilePath, $newFilePath)
P.S。将文件作为后期流发送是一个非常糟糕的主意。