上传php脚本问题

发布于 2024-11-06 18:47:31 字数 2048 浏览 0 评论 0原文

使用 Expression Web,想要将图像上传到我的网页,即使是用脚本编写,也不会收到错误消息。设置上传文件夹与大家共享。 php.ini 设置为 file_uploads=on;upload_max_size=128M。在 Firefox 上,脚本超时,在 IE 上,“Internet Explorer 无法显示页面”。

几周来我一直在寻找答案。即使我是 php 的新手,我尝试过的所有其他脚本都可以工作。这个上传的事情让我摸不着头脑。最近的上传脚本尝试:

if (isset($_POST['submitted'])) {

    // Check for an uploaded file:
    if (isset($_FILES['upload'])) {

        // Validate the type. Should be JPEG or PNG.
        $allowed = array ('image/pjpeg', 'image/jpeg', 'image/JPG', 'image/X-PNG', 'image/PNG', 'image/png', 'image/x-png');
        if (in_array($_FILES['upload']['type'], $allowed)) {

            $target_path = "C:/uploads/";
            $name=$_FILES['upload']['name'];
            $error=$_FILES['upload']['error'];
            $tmp_name=$_FILES['upload']['tmp_name'];

            if($error==UPLOAD_ERR_OK){

               if($_FILES['upload']['size']>0){

               move_uploaded_file($tmp_name, $target_path.$name); 

            print ("The file ".$name." has been uploaded.\n");

             } else{

             print ("There was an error uploading the file, please try again!\n");

             }

             }elseif ($error==UPLOAD_ERR_NO_FILE) {

             print("No files specified.\n");

             }else{

             print("Upload failed.\n");

             }
             print("\n");


        }//End of !in_array IF.

    } // End of isset($_FILES['upload']) IF.


} // End of the submitted conditional.
?>

<div>
  <form action="photogallery.php" enctype="multipart/form-data" method="post">
  <input type="hidden" name="MAX_FILE_SIZE"  value="524288" />
  <p><b>File:</b><input name="upload" type="file" /></p>
  <input name="submit" type="submit" value="Submit" />
  <input name="submitted" type="hidden" value="TRUE" />
  </form>
</div>

我正在尝试将图像上传到我的目录中的上传文件夹中,然后显示在我的网页上。我已手动将图像放入文件夹中,它们将通过链接显示在我的网页上,但使用这些脚本无法正常工作。我的网站尚未上线,正在开发中,我正在使用 XAMPP 和 Microsoft 开发服务器。

Using Expression Web, want to upload images to my web pages, not getting error messages even though written in script. Set the upload folder to share with everyone. php.ini is set to file_uploads=on;upload_max_size=128M. On firefox the script times out and on IE "Internet Explorer can not display page."

I have been searching for answers for weeks. Every other script I have tried works even though I am a novice at php. This upload thing has gotten me scratching my head. Most recent upload script attempt:

if (isset($_POST['submitted'])) {

    // Check for an uploaded file:
    if (isset($_FILES['upload'])) {

        // Validate the type. Should be JPEG or PNG.
        $allowed = array ('image/pjpeg', 'image/jpeg', 'image/JPG', 'image/X-PNG', 'image/PNG', 'image/png', 'image/x-png');
        if (in_array($_FILES['upload']['type'], $allowed)) {

            $target_path = "C:/uploads/";
            $name=$_FILES['upload']['name'];
            $error=$_FILES['upload']['error'];
            $tmp_name=$_FILES['upload']['tmp_name'];

            if($error==UPLOAD_ERR_OK){

               if($_FILES['upload']['size']>0){

               move_uploaded_file($tmp_name, $target_path.$name); 

            print ("The file ".$name." has been uploaded.\n");

             } else{

             print ("There was an error uploading the file, please try again!\n");

             }

             }elseif ($error==UPLOAD_ERR_NO_FILE) {

             print("No files specified.\n");

             }else{

             print("Upload failed.\n");

             }
             print("\n");


        }//End of !in_array IF.

    } // End of isset($_FILES['upload']) IF.


} // End of the submitted conditional.
?>

<div>
  <form action="photogallery.php" enctype="multipart/form-data" method="post">
  <input type="hidden" name="MAX_FILE_SIZE"  value="524288" />
  <p><b>File:</b><input name="upload" type="file" /></p>
  <input name="submit" type="submit" value="Submit" />
  <input name="submitted" type="hidden" value="TRUE" />
  </form>
</div>

I am trying to upload images into upload folder in my directory and then display on my web page. I have put images into the folder manually and they will show on my webpage via a link but it wont work using these scripts. My site is not live it is in developement and I am using XAMPP and Microsoft development Server.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盛夏已如深秋| 2024-11-13 18:47:31

实际的错误很难说。但是您的脚本中有几点需要提及(典型的上传脚本问题):

  • ['name'] 常量可能为空,这将解释上传错误。
  • 但它也可能被篡改,并包含诸如 "../Windows/System.ini" 之类的内容,这对您的脚本来说不太好,因为您使用未经过滤的它作为输出文件名。
  • MIME ['type'] 同样不可靠。但这可能不是这里的问题。至少使用 strtolower() 来代替列出多个 MIME 替代方案。
  • 最后启用 error_reporting(E_ALL); 这将告诉您 move_upload_file() 权限问题是否是问题所在。

我建议您从一个简单的开始

move_uploaded_file($_FILES["upload"]["tmp_name"], "C:/uploads/test.1");

,然后逐渐根据您的其他条件进行扩展,以找到上传问题的原因。

The actual error is hard to tell. But there are a few points to mention on your script (typical upload script problems):

  • The ['name'] constanct could be empty, which would explain an upload error.
  • But it could also be tampered with, and contain things like "../Windows/System.ini", which wouldn't be so good for your script since you use it unfiltered as output filename.
  • The MIME ['type'] is likewise unreliable. But that's likely not the problem here. At the very least use strtolower() on it instead of listing multiple MIME alternatives.
  • Lastly enable error_reporting(E_ALL); which will tell you if move_upload_file() permission issues are the problem.

I would suggest that you start with a simple

move_uploaded_file($_FILES["upload"]["tmp_name"], "C:/uploads/test.1");

And then gradually extend it with your other conditions to find the cause of your upload issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文