繁体   English   中英

move_uploaded_file()函数在服务器上不起作用?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM