我有一个网站,允许用户使用PHP move_uploaded_file()和PHP表单上传图像。在本地开发中,上传工作正常,图像从用户计算机复制到' / library / uploads'站点目录中的文件夹。
虽然我已经将网站放在网络服务器上,但它不再将图像添加到文件夹中。这是我的代码:
PHP move_uploaded_file()
代码:
if($_FILES['myfile']['name'])
{
//if no errors...
if(!$_FILES['myfile']['error'])
{
//now is the time to modify the future file name and validate the file
$new_file_name = strtolower($_FILES['myfile']['tmp_name']); //rename file
if($_FILES['myfile']['size'] > (102400000)) //can't be larger than 100 MB
{
$valid_file = false;
$message = 'Oops! Your file\'s size is to large.';
}
//if the file has passed the test
if($valid_file)
{
//move it to where we want it to be
move_uploaded_file($_FILES['myfile']['tmp_name'], '/library/uploads/'.$_FILES['myfile']['name']);
$message = 'Congratulations! Your file was accepted.';
}
}
//if there is an error...
else
{
//set that to be the returned message
$message = 'Ooops! Your upload triggered the following error: '.$_FILES['myfile']['error'];
}
}
表格代码:
<form class="search-form__form" method="POST" enctype="multipart/form-data">
<input type="file" name="myfile" />
<input class="search-form__button" name="submit-service" type="submit" value="Submit Service ">
<div class="search-form__form-message">
<p class="search-form__error-message"><?php echo $message; ?></p>
</div>
</form>
这是我的网络服务器目录:
答案 0 :(得分:1)
该函数未调用,因为未定义变量$ valid_file 首先在顶部将其定义为 $ valid_file = true;
然后在if条件检查
if($valid_file == true){
// do upload file
} else{
// error show (Because if it goes in anather if it will go the the false and file will not upload)
} here i update your code
$valid_file = true;
if($_FILES['myfile']['name'])
{
//if no errors...
if(!$_FILES['myfile']['error'])
{
//now is the time to modify the future file name and validate the file
$new_file_name = strtolower($_FILES['myfile']['tmp_name']); //rename file
if($_FILES['myfile']['size'] > (102400000)) //can't be larger than 100 MB
{
$valid_file = false;
$message = 'Oops! Your file\'s size is to large.';
}
//if the file has passed the test
if($valid_file == true)
{
//move it to where we want it to be
move_uploaded_file($_FILES['myfile']['tmp_name'], '/library/uploads/'.$_FILES['myfile']['name']);
$message = 'Congratulations! Your file was accepted.';
}
}
//if there is an error...
else
{
//set that to be the returned message
$message = 'Ooops! Your upload triggered the following error: '.$_FILES['myfile']['error'];
}
}