PHP上传问题

发布于 2024-07-22 06:40:21 字数 1247 浏览 11 评论 0原文

我使用了一些脚本来开始在我的开发计算机上上传文件。 问题是,尽管此操作预期很容易,但每当我尝试上传图像时,Apache 似乎都会超时。 上传设置为Ontmp 目录在php.ini 中设置。

我尝试从 Google 上传主 gif,这是一张 8.36KB 图像。 它应该没问题,并且完全在 PHP 上传功能的限制之内。

这是脚本的副本。 应该有一个简单的修复方法。 根据要求,我将波形符更改为实际目录。

<?php 

if (!isset($_GET['upload'])) { ?>
  <form method="post" action="index.php?upload=true" enctype="multipart/form-data"> 
  <input type="file" name="file" class="form">
  <input name="submit" type="submit">
  </form>
<? } else  if (isset($_GET['upload']) && $_GET['upload'] == 'true') {
  $url = $_FILES['file']['name'];
    $move = move_uploaded_file($_FILES['file']['tmp_name'], "/Users/<username>/Sites/file.jpg");
    if ($move) {
        echo "Success!";
    } else { 
        echo "Err..."
    } 
} ?>

谢谢, 丹

编辑:

我在一些答案的帮助下修复了它,我将标记其中一个。

这里有一些事情导致了这种行为。

  1. images 目录的权限未设置为允许 _www 用户访问它。 chmod -R 777 images 似乎可以修复它,以及 sudo chown _www images

  2. 表单输出可能已损坏 PHP 脚本本身。 正如所建议的,ECHO <<< ...END 我认为有帮助。

I've worked with a few scripts to begin uploading files on my development machine. Problem is, despite the expected ease of this operation, Apache seems to time-out whenever I try to upload an image. Uploading is set to On and the tmp directory is set in php.ini.

I tried uploading the main gif from Google, an 8.36KB image. It should be fine and well within the limits to PHPs uploading capabilities.

Here is a copy of the script. There should be an easy fix. As requested, I changed the tilde to an actual directory.

<?php 

if (!isset($_GET['upload'])) { ?>
  <form method="post" action="index.php?upload=true" enctype="multipart/form-data"> 
  <input type="file" name="file" class="form">
  <input name="submit" type="submit">
  </form>
<? } else  if (isset($_GET['upload']) && $_GET['upload'] == 'true') {
  $url = $_FILES['file']['name'];
    $move = move_uploaded_file($_FILES['file']['tmp_name'], "/Users/<username>/Sites/file.jpg");
    if ($move) {
        echo "Success!";
    } else { 
        echo "Err..."
    } 
} ?>

Thanks,
Dan

EDIT:

I fixed it, with help from a few of the answers, to one of which I will mark.

A few things here were causing this behavior.

  1. Permissions on the images directory were not set to allow the _www user to access it. A chmod -R 777 images seemed to fix it, as well as a sudo chown _www images.

  2. The form output may have been corrupting the PHP script itself. As suggested, an ECHO <<< ...END helped, I think.

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

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

发布评论

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

评论(4

淡淡離愁欲言轉身 2024-07-29 06:40:21

是什么让您相信 Apache 正在超时,而不是在某些方面彻底失败? 因为我突然意识到您正在尝试将文件移动到 ~/file.jpg,我几乎可以肯定这是行不通的; ~ 是一种通常只在 shell 内有意义的构造,除非 PHP 的一个奇怪的晦涩功能正在像这样的上下文中处理它。 无论如何,尝试放置实际目录。

What is it that leads you to believe that Apache is timing out rather than, say, outright failing in some way? Because what leaps out at me is that you're trying to move the file to ~/file.jpg, which I'm nearly certain will not work; ~ is a construct that only normally has meaning inside shells, unless one of PHP's freakish obscure features is processing it in contexts like this. Anyway, try putting the actual directory.

绅刃 2024-07-29 06:40:21

这很可能是文件大小的问题和/或 Apache 用户与指定目录之间的权限问题。 例如,确保 Apache 实例不在用户(nobody)下运行。

评论混沌:
他是对的,波形符 (~) 可能会导致问题,但可能不会导致超时; 它会显示警告。 即使它确实在您的系统上运行,如果 Apache 用户(即 www)没有设置有效的主目录,它也可能会将文件存储到意外的目录中或遇到一些问题。

This is more than likely an issue with the size of the file and/or a permission issue between the Apache user and the directory specified. For instance make sure the Apache instance is not running under user (nobody).

Comment to chaos:
He is right the tilde (~) can cause issues, but would probably not cause a timeout; it would display a warning. Even if it does work on your system it would probably deposit the file into an unexpected directory or run into some issues if the Apache user (ie www) does not have a valid home directory set.

╰◇生如夏花灿烂 2024-07-29 06:40:21

如果问题是文件大小,请将以下行添加到您的 php.ini 文件中,它应该可以工作:

upload_max_filesize = 500M ;
post_max_size = 500M ;

If the issue is filesize, add the following lines to your php.ini file and it should work:

upload_max_filesize = 500M ;
post_max_size = 500M ;
﹉夏雨初晴づ 2024-07-29 06:40:21

PHP 默认情况下在页面上有 30 秒的超时时间。 因此,如果您的上传时间超过 30 秒,就会失败。 在 php.ini 中设置超时或将以下代码放在文件顶部。

ini_set(max_execution_time, 90);

第二个参数表示页面超时之前的时间(以秒为单位)。 将其设置为您认为合适的任何时间。 此外,混乱是正确的,因为“~”是一种通常只在 shell 内部才有意义的结构。

回复:https://www.php.net/manual/en/ini。 list.php

编辑:
问题是您在条件中间重新打开了标签。 尝试你的代码我得到一个语法错误。 奇怪的是您竟然能够看到任何网络表单。 这是固定代码(对我有用)。

<?php 
if (!isset($_GET['upload'])) { 
ECHO <<<END
  <form method="post" action="index.php?upload=true" enctype="multipart/form-data"> 
  <input type="file" name="file" class="form">
  <input name="submit" type="submit">
  </form>
END;
 } else  if (isset($_GET['upload']) && $_GET['upload'] == 'true') {
    $url = $_FILES['file']['name'];
    $move = move_uploaded_file($_FILES['file']['tmp_name'], "/Users/<username>/Sites/file.jpg");
    if ($move) {
        echo "Success!";
    } else { 
        echo "Err...";
    } 
} ?>

PHP by default has a 30 second timeout on the page. So if your upload takes longer than 30 seconds it will fail. Set the timeout either in your php.ini or put the following code at the top of the file.

ini_set(max_execution_time, 90);

The second argument represents the time in seconds before the page will timeout. Set it to whatever time you feel is appropriate. Also, chaos is correct in that '~' is a construct that commonly has meaning only inside shells.

Re: https://www.php.net/manual/en/ini.list.php

EDIT:
The problem is that you reopened the tag in the middle of a conditional. Trying your code I get a syntax error. It's strange that you were able to see any web form. This is the fixed code (that works for me).

<?php 
if (!isset($_GET['upload'])) { 
ECHO <<<END
  <form method="post" action="index.php?upload=true" enctype="multipart/form-data"> 
  <input type="file" name="file" class="form">
  <input name="submit" type="submit">
  </form>
END;
 } else  if (isset($_GET['upload']) && $_GET['upload'] == 'true') {
    $url = $_FILES['file']['name'];
    $move = move_uploaded_file($_FILES['file']['tmp_name'], "/Users/<username>/Sites/file.jpg");
    if ($move) {
        echo "Success!";
    } else { 
        echo "Err...";
    } 
} ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文