php move_上传的_文件()
因此,我正在测试w3schools网站上的move_uploaded_file()php脚本。 这是我的密码php move_上传的_文件(),php,file-upload,upload,Php,File Upload,Upload,因此,我正在测试w3schools网站上的move_uploaded_file()php脚本。 这是我的密码 if ($_FILES["file"]["size"] < 2000000) { if ($_FILES["file"]["error"] > 0) echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; else { echo "Upload:
if ($_FILES["file"]["size"] < 2000000)
{
if ($_FILES["file"]["error"] > 0)
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("/var/www/upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
elseif(move_uploaded_file($_FILES["file"]["tmp_name"], "/var/www/upload/".$fileName))
echo "Stored in: " . "/var/www/upload/".$fileName;
}
}
else
echo "Invalid file";
if($\u文件[“文件”][“大小”]<2000000)
{
如果($\u文件[“文件”][“错误”]>0)
回显“返回代码:”.$\u文件[“文件”][“错误”]。“
”;
其他的
{
echo“上传:.”文件[“文件”][“名称”]。“
”;
回显“类型:”.$\u文件[“文件”][“类型”]。“
”;
回显“大小:”($_文件[“文件”][“大小”]/1024)。“Kb
”;
回显“临时文件:”.$\u文件[“文件”][“tmp\u名称”]。“
”;
如果(文件存在(“/var/www/upload/”$\u文件[“文件”][“名称”]))
{
echo$\u文件[“文件”][“名称”]。“已存在。”;
}
elseif(移动上传的文件($文件[“文件”][“tmp文件名”],“/var/www/upload/”$fileName))
echo“存储在:”../var/www/upload/”$fileName中;
}
}
其他的
回显“无效文件”;
问题是
如果(move_upload_file($_FILES[“file”][“tmp_name”],“/var/www/upload/”$fileName))
始终返回false,但文件似乎存储在服务器上的tmp
文件夹中(例如:/tmp/php8rrKoW
)。当我检查tmp
文件夹时,文件不在那里。(它应该在脚本执行完毕后被删除。)我也没有看到/php8rrkoW
文件夹。我不确定它是否应该在那里。我使用chmod
将tmp
文件夹和/var/www/upload/
的权限设置为777
,但我不确定是否应该将所有者设置为apache
。因此,我想知道为什么文件没有复制到/var/www/upload
,以及是否有办法测试这一点。似乎您应该使用/upload/而不是/var/www/upload/,因为您已经在可访问网站的主目录中
你也可以参考
或API:
让我知道这是否有效您似乎应该使用./upload/而不是/var/www/upload/,因为您已经在可访问网站的主目录中 你也可以参考 或API:
让我知道这是否有效这是我前几天为另一个问题制作的一个基本图像上传类,简单易用,也许你会觉得有趣
<?php
error_reporting(E_ALL); //Will help you debug a [server/path/permission] issue
Class uploadHandler{
public $upload_path;
public $full_path;
public $name;
public $size;
public $ext;
public $output;
public $input;
public $prefix;
private $allowed;
function upload(){
if($_SERVER['REQUEST_METHOD'] == 'POST'){
if(isset($_FILES[$this->input]['error'])){
if($_FILES[$this->input]['error'] == 0){
$this->name = basename($_FILES[$this->input]['name']);
$file_p = explode('.', $this->name);
$this->ext = end($file_p);
$this->full_path = rtrim($this->upload_path,'/').'/'.preg_replace('/[^a-zA-Z0-9.-]/s', '_', $this->prefix.'_'.$file_p[0]).'.'.$this->ext;
$info = getimagesize($_FILES[$this->input]['tmp_name']);
$this->size = filesize($_FILES[$this->input]['tmp_name']);
if($info[0]>$this->allowed['dimensions']['width'] || $info[1] > $this->allowed['dimensions']['height']){
$this->output = 'File dimensions too large!';
}else{
if($info[0] > 0 && $info[1] > 0 && in_array($info['mime'],$this->allowed['types'])){
move_uploaded_file($_FILES[$this->input]['tmp_name'],$this->full_path);
$this->output = 'Upload success!';
}else{
$this->output = 'File not supported!';
}
}
}else{
if($_FILES[$this->input]['error']==1){$this->output = 'The uploaded file exceeds the upload_max_filesize directive!';}
if($_FILES[$this->input]['error']==2){$this->output = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in our HTML form!';}
if($_FILES[$this->input]['error']==3){$this->output = 'The uploaded file was only partially uploaded!';}
if($_FILES[$this->input]['error']==4){$this->output = 'No file was uploaded!';}
if($_FILES[$this->input]['error']==6){$this->output = 'Missing a temporary folder!';}
if($_FILES[$this->input]['error']==7){$this->output = 'Failed to write uploaded file to disk!';}
if($_FILES[$this->input]['error']==8){$this->output = 'A PHP extension stopped the file upload!';}
}
}
}
}
function setPath($var){
$this->upload_path = $var;
}
function setAllowed($var=array()){
$this->allowed = $var;
}
function setFilePrefix($var){
$this->prefix = preg_replace('/[^a-zA-Z0-9.-]/s', '_', $var);
}
function setInput($var){
$this->input = $var;
}
}
//Start class
$upload = new uploadHandler();
//Set path
$upload->setPath('./');
//Prefix the file name
$upload->setFilePrefix('user_uploads');
//Allowed types
$upload->setAllowed(array('dimensions'=>array('width'=>200,'height'=>200),
'types'=>array('image/png','image/jpg','image/gif')));
//form property name
$upload->setInput('myfile');
//Do upload
$upload->upload();
//notice
if(isset($upload->output)){
echo $upload->output;
}
?>
<form action="" method="POST" enctype="multipart/form-data">
<!--1 MB = 1048576 bytes-->
<input type="hidden" name="MAX_FILE_SIZE" value="1048000" />
<p>Upload your image:<input type="file" name="myfile"><input type="submit" value="Upload"></p>
</form>
上载您的图像:
这是我前几天为另一个问题制作的一个基本图像上传类,简单易用,也许你会觉得有趣
<?php
error_reporting(E_ALL); //Will help you debug a [server/path/permission] issue
Class uploadHandler{
public $upload_path;
public $full_path;
public $name;
public $size;
public $ext;
public $output;
public $input;
public $prefix;
private $allowed;
function upload(){
if($_SERVER['REQUEST_METHOD'] == 'POST'){
if(isset($_FILES[$this->input]['error'])){
if($_FILES[$this->input]['error'] == 0){
$this->name = basename($_FILES[$this->input]['name']);
$file_p = explode('.', $this->name);
$this->ext = end($file_p);
$this->full_path = rtrim($this->upload_path,'/').'/'.preg_replace('/[^a-zA-Z0-9.-]/s', '_', $this->prefix.'_'.$file_p[0]).'.'.$this->ext;
$info = getimagesize($_FILES[$this->input]['tmp_name']);
$this->size = filesize($_FILES[$this->input]['tmp_name']);
if($info[0]>$this->allowed['dimensions']['width'] || $info[1] > $this->allowed['dimensions']['height']){
$this->output = 'File dimensions too large!';
}else{
if($info[0] > 0 && $info[1] > 0 && in_array($info['mime'],$this->allowed['types'])){
move_uploaded_file($_FILES[$this->input]['tmp_name'],$this->full_path);
$this->output = 'Upload success!';
}else{
$this->output = 'File not supported!';
}
}
}else{
if($_FILES[$this->input]['error']==1){$this->output = 'The uploaded file exceeds the upload_max_filesize directive!';}
if($_FILES[$this->input]['error']==2){$this->output = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in our HTML form!';}
if($_FILES[$this->input]['error']==3){$this->output = 'The uploaded file was only partially uploaded!';}
if($_FILES[$this->input]['error']==4){$this->output = 'No file was uploaded!';}
if($_FILES[$this->input]['error']==6){$this->output = 'Missing a temporary folder!';}
if($_FILES[$this->input]['error']==7){$this->output = 'Failed to write uploaded file to disk!';}
if($_FILES[$this->input]['error']==8){$this->output = 'A PHP extension stopped the file upload!';}
}
}
}
}
function setPath($var){
$this->upload_path = $var;
}
function setAllowed($var=array()){
$this->allowed = $var;
}
function setFilePrefix($var){
$this->prefix = preg_replace('/[^a-zA-Z0-9.-]/s', '_', $var);
}
function setInput($var){
$this->input = $var;
}
}
//Start class
$upload = new uploadHandler();
//Set path
$upload->setPath('./');
//Prefix the file name
$upload->setFilePrefix('user_uploads');
//Allowed types
$upload->setAllowed(array('dimensions'=>array('width'=>200,'height'=>200),
'types'=>array('image/png','image/jpg','image/gif')));
//form property name
$upload->setInput('myfile');
//Do upload
$upload->upload();
//notice
if(isset($upload->output)){
echo $upload->output;
}
?>
<form action="" method="POST" enctype="multipart/form-data">
<!--1 MB = 1048576 bytes-->
<input type="hidden" name="MAX_FILE_SIZE" value="1048000" />
<p>Upload your image:<input type="file" name="myfile"><input type="submit" value="Upload"></p>
</form>
上载您的图像:
要将文件移动到的目标目录应由Web服务器用户写入。另外,不要忘记某些Web服务器在changeroot内部运行,因此可能不需要部分目标路径。PHP帮助文档还说,您应该检查HTLM表单,以确保它是enctype='multipart/formdata'
最后一点:$filename是在哪里定义的 要将文件移动到的目标目录应由Web服务器用户写入。另外,不要忘记某些Web服务器在changeroot内部运行,因此可能不需要部分目标路径。PHP帮助文档还说,您应该检查HTLM表单,以确保它是enctype='multipart/formdata'
最后一点:$filename是在哪里定义的 您的路径必须由“/home/usr name”组成 尝试将“/home/your username”添加到“/var/www/upload”的开头
为了得到一个想法,在根目录中添加一个info.php,在浏览器中打开它,然后在“加载的配置文件”下查看 您的路径必须由“/home/usr name”组成 尝试将“/home/your username”添加到“/var/www/upload”的开头
为了得到一个想法,在根目录中添加一个info.php,在浏览器中打开它,然后在“加载的配置文件”下查看 您是否启用了错误报告(E_ALL)以及您收到了哪些错误?尝试使用
请求index.php
是否启用了错误报告(E_ALL)以及您收到了哪些错误?尝试使用请求index.php
我尝试了tizag的代码,并将其更改为./upload,但我收到了“上载文件时出错,请重试!“。我不确定文件是否已传输到临时文件夹。我尝试了tizag中的代码并将其更改为./upload,但我得到了”上载文件时出错,请重试!“。我不确定该文件是否已传输到临时文件夹。我收到“上载成功”消息,但找不到上载的文件。此文件将存储在哪个位置?我已在/var/www/html/test/index.php中部署了代码。我收到“上载成功”消息,但找不到上载的文件。此文件将存储在哪个位置?我已部署代码位于/var/www/html/test/index.php。