[英]SERVER:move_uploaded_file doesn't work in freewebhostingarea
[英]move_uploaded_file() function doesn't work on server?
我有一個網站,允許用戶使用PHP move_uploaded_file()和PHP表單上傳圖像。 在本地開發中,上傳工作正常,並且圖像從用戶計算機復制到站點目錄內的“ / library / uploads”文件夾中。
盡管將站點放置在Web服務器上后,它不再將圖像添加到文件夾中。 這是我的代碼如下:
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>
這是我的Web服務器目錄:
該函數未調用,因為未首先在頂部定義變量$ valid_file,將其定義為$ valid_file = true;
然后在條件檢查中
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.