[英]move_uploaded_file() function doesn't work on server?
I have a website that allows a user to upload an image using PHP move_uploaded_file() and a PHP form. 我有一个网站,允许用户使用PHP move_uploaded_file()和PHP表单上传图像。 On local development the upload works fine and the image is copied from the users computer into the '/library/uploads' folder within the site directory. 在本地开发中,上传工作正常,并且图像从用户计算机复制到站点目录内的“ / library / uploads”文件夹中。
Although once I've put the site on the web server it doesn't add the image to the folder anymore. 尽管将站点放置在Web服务器上后,它不再将图像添加到文件夹中。 Here's my code below: 这是我的代码如下:
PHP move_uploaded_file()
code: 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 code: 表单代码:
<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>
And here's my web server directory: 这是我的Web服务器目录:
The function is not calling because the variable $valid_file is not define first at the top define it as $valid_file = true; 该函数未调用,因为未首先在顶部定义变量$ valid_file,将其定义为$ valid_file = true;
then in if condition check 然后在条件检查中
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'];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.