努力上传文件

时间:2019-05-20 15:06:13

标签: php

编辑

好的,所以我今天早上已经做完了:

我执行了以下命令:

sudo apt install php7.3-curl && sudo apt install php7.3-enchant && sudo apt install php7.3-gmp && sudo apt install php7.3-intl && sudo apt install php7.3-mbstring && sudo apt install php7.3-opcache && sudo apt install php7.3-pspell && sudo apt install php7.3-snmp && sudo apt install php7.3-sybase && sudo apt install php7.3-xmlrpc && sudo apt install php7.3-bcmath && sudo apt install php7.3-cli && sudo apt install php7.3-dba && sudo apt install php7.3-fpm && sudo apt install php7.3-imap && sudo apt install php7.3-json && sudo apt install php7.3-mysql && sudo apt install php7.3-pgsql && sudo apt install php7.3-readline && sudo apt install php7.3-soap && sudo apt install php7.3-tidy && sudo apt install php7.3-xsl && sudo apt install php7.3-bz2 && sudo apt install php7.3-common && sudo apt install php7.3-dev && sudo apt install php7.3-gd && sudo apt install php7.3-interbase && sudo apt install php7.3-ldap && sudo apt install php7.3-odbc && sudo apt install php7.3-phpdbg && sudo apt install php7.3-recode && sudo apt install php7.3-sqlite3 && sudo apt install php7.3-xml && sudo apt install php7.3-zip

然后修改我的php.ini,以注释掉我创建的自定义设置tmp_dir。然后重启我的服务器,哇!现在可以正常使用了。

所以我的猜测是,如果我不得不猜测,-common可能是某个地方缺少了一个模块?不确定,从来没有真正研究过哪个模块在做什么(将来可能应该避免这些问题)。

我将去做一些研究,并在缩小哪个模块修复它的范围后会给出答案。

编辑2

我非常有信心,我缩小了负责解决问题的模块的范围。看来我没有安装php7.3-curl。这是该模块的说明,摘自docs:

  

PHP支持libcurl,这是Daniel Stenberg创建的一个库,它使您可以通过许多不同类型的协议连接到许多不同类型的服务器并与之通信。 libcurl当前支持http,https,ftp,gopher,telnet,dict,file和ldap协议。 libcurl还支持HTTPS证书,HTTP POST,HTTP PUT,FTP上传(也可以使用PHP的ftp扩展名完成),基于HTTP表单的上传,代理,Cookie和用户+密码验证。

其中的file(在受支持的协议列表下)。

但是,卸载此模块并重新启动我的Web服务器并没有改变,但仍然成功。

我想知道在后台是否发生了一些奇怪的缓存事情,我无法弄清楚。

原始帖子

我具有上传图像并将一些数据插入数据库的功能。它是由AJAX事件触发的,并且一切正常,直到尝试上传图像为止。

这是我的(简体)表单HTML:

<form method="post"
      class="form-horizontal"
      id="add-dealers-form"
      enctype="multipart/form-data">
    <input type="file" accept="image/*" name="img" id="img" />
</form>

<button type="button" class="btn btn-success" id="submit-add-model-btn">
    <span>Update Model</span>
</button>

(相关的)AJAX:

$('#submit-add-model-btn').click(function()
{
    let form = document.getElementById('add-dealers-form'),
        formData = new FormData(form);

    $.ajax({
        type: 'post',
        url: '/wp-content/plugins/my-plugin/app/scripts/admin/add-model.php',
        data: formData,
        cache: false,
        contentType: false,
        processData: false,
        success: function(res) {console.log(res); alert(res)},
        error: function(res)
        {
            alert('Something went wrong. Please try again.');
            console.log(res)
        }
    })
})

(相关的)PHP:

if (!$img['error']) {
    if (move_uploaded_file($tmp, $_SERVER['DOCUMENT_ROOT']. '/wp-content/plugins/my-plugin/images/models/'. $name)) {
        $wpdb->query($wpdb->prepare(
            $sql,
            array($_POST['name'], $_POST['sku'], $_POST['type'], $name)
        ));
    } else {
        $response['110'] = 'There was a problem with the upload. Please try again.';
    }
} else {
    $response['110'] = 'There was a problem with the upload. Please try again.';
}

我也尝试过:

if ($img['error'] == 0)

我还尝试使用copy()

在条件之前添加print_r()表示已填充$_FILES$_POST。所以在那里没有问题。

这是要抓住的地方(我的文件夹权限):

drwxrwxrwx  3 www-data www-data 4096 May 20 13:56 images/

然后在其中建模:

drwxrwxrwx 2 www-data www-data 4096 May 20 13:56 models/

因此,它具有完全不安全的,开放的权限,但是它不起作用。我已经检查了本地错误日志,再也没有任何内容。我已在脚本中启用了错误报告功能-仍然没有。

“我的网络”标签显示该请求返回200状态。它还显示了我设置的错误消息。

我也尝试过删除AJAX步骤并直接转到PHP脚本-再次没有。

我经历了一些SO帖子并尝试了替代解决方案,但没有成功。我不确定我的脚本有什么问题,它具有enctype,具有用于图像发送的所需Ajax设置,并且具有完全权限,我缺少什么?

我要上传的文件为70kb(因此不会引起大小问题)

我已经测试了我知道可以工作的其他代码(可以在生产服务器上工作)-因此绝对是服务器级别的。让我知道您认为(配置明智)有用的内容,我将添加到Q中。

服务器端设置

可能有用的.ini设置:

memory_limit = -1
upload_max_size = 2M
post_max_size = 8M
upload_tmp_dir = /tmp/php
sys_temp_dir = /tmp/php

Apache

Server version: Apache/2.4.29 (Ubuntu)
Server built:   2019-04-03T13:22:37


Loaded Modules:
 core_module (static)
 so_module (static)
 watchdog_module (static)
 http_module (static)
 log_config_module (static)
 logio_module (static)
 version_module (static)
 unixd_module (static)
 access_compat_module (shared)
 alias_module (shared)
 auth_basic_module (shared)
 authn_core_module (shared)
 authn_file_module (shared)
 authz_core_module (shared)
 authz_host_module (shared)
 authz_user_module (shared)
 autoindex_module (shared)
 deflate_module (shared)
 dir_module (shared)
 env_module (shared)
 filter_module (shared)
 mime_module (shared)
 mpm_prefork_module (shared)
 negotiation_module (shared)
 php7_module (shared)
 proxy_module (shared)
 proxy_fcgi_module (shared)
 reqtimeout_module (shared)
 rewrite_module (shared)
 setenvif_module (shared)
 status_module (shared)

PHP

PHP 7.3.5-1+ubuntu18.04.1+deb.sury.org+1 (cli) (built: May  3 2019 10:00:24) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.5, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.5-1+ubuntu18.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

/tmp的权限

drwxrwxrwx  22 root root      12288 May 20 17:29 tmp/

/tmp/php的权限

drwxrwxr-x  2 www-data www-data    4096 May 20 16:28 php/

1 个答案:

答案 0 :(得分:-1)

此PHP / HTML代码为我工作。根据需要进行修改。希望对您有所帮助::)

<!DOCTYPE html><html><body><style>
*{font-family:corbel;padding:0.8em;margin-left:2em;background:#9dc;}
</style>
<form action="files.php" method="post" enctype="multipart/form-data">
  Files (not videos though):
  <input type="file" name="fileToUpload" id="fileToUpload">
  <input type="submit" value="Upload Image" name="submit">
</form></body></html>

<?php
$target_dir = "";
$t_f = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$stat = 1;
$f_Type = strtolower(pathinfo($t_f,PATHINFO_EXTENSION));

/* Check if file already exists
if(file_exists($t_f)) {echo "Sorry, file already exists.";$stat = 0;}
*/
//Debugging
echo"<p style='color:#777'>";
print_r($_FILES["fileToUpload"]);
echo'</p>';

// Check file size. 0.5MB Max.
if($_FILES["fileToUpload"]["size"] > 500000) {
  echo "Sorry, your file is too large.";
  $stat = 0;}

/* Allow certain file formats
if($f_Type != "jpg" && $f_Type != "png" && $f_Type != "jpeg"
&& $f_Type != "gif" ) {
  echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  $stat = 0;}
 Check if $stat is set to 0 by an error*/
if($stat == 0) {
  echo "Sorry, your file was not uploaded.";}
// if everything is ok, try to upload file
else {
  if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $t_f)) {
    echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";}
else { echo "Welcome to Aera23's uploader. This message should disappear if the file is uploaded";}}
?>

?>