为什么我上传的所有文件都与404错误?

时间:2018-03-15 22:54:53

标签: php wordpress

因此,对于练习,我正在制作一个允许用户将文件上传到我的网站的小表格。我正在使用wordpress,无论我在谷歌搜索什么,我只是获得插件的链接。由于我在练习中这样做,使用插件会破坏目的。

到目前为止,有效的是,当提交文件时,在wordpress安装的根目录中创建一个文件夹(如果它不存在)。然后,在该文件夹中,为当前用户创建另一个。所以现在服务器中有一个/ uploads /文件夹,文件会在那里上传。 我还在用户元数据中放了一个数组。该数组在每个索引处都包含一个关联子数组,其中包含文件名,路径和类型。我检查了phpMyAdmin,用户元数据更新也有效。

什么行不通的是所有文件都是不可能的。我从用户meta获取数组,然后为每个子数组I echo

<a href="[file_path]">[file_name] -> [file_type]</a>
<img src="[file_path]">Could not load image: [file_name]</img>

第一个用于文本文件,第二个用于图像。如果我单击文本文件的链接,它会给我404错误,URL看起来没问题,图像也不会显示。

下面是代码中所有光荣的混乱。我甚至尝试使用umask将所有权限设置为0777(在文件管理器中,上传文件夹获得此权限以及特定用户,但文件获得0644),因为我认为这可能是导致错误的原因。我根本不知道在哪里可以解决这个问题......

<?php

  //response generation function
  $response = "";

  //function to generate response
  function my_contact_form_generate_response($type, $message){
    global $response;

    if($type == "success") $response = "<div class='success'>{$message}</div>";
    else $response = "<div class='error'>{$message}</div>";

  }

function check_form() {
    // Get & verify user
    $current_user = wp_get_current_user();
    if ( !($current_user instanceof WP_User) ) {
        return 'Current user is invalid';
    }

    // Verify/create root upload directory
    $upload_path = ABSPATH . 'uploads/';
    if (!file_exists($upload_path)) {
        $old_mask = umask(0);
        if (!mkdir($upload_path, 0777, true)) {
            return 'Could not create root upload directory';
        }
        umask($old_mask);
    }

    // Verify/create user upload directory
    $current_user_dname = (string)$current_user->display_name;
    $current_user_id = (string)$current_user->ID;
    $upload_path .= $current_user_dname . $current_user_id;
    if ( !file_exists( $upload_path ) ) {
        // Create directory
        $old_mask = umask(0);
        if ( !mkdir( $upload_path, 0777, true ) ) {
            return 'Could not create user upload directory.';
        }
        umask($old_mask);

        // Register in user meta data
        update_user_meta($current_user_id , 'uploaded_files', array());
    }

    $allowed_ftypes = array(
        '.jpg', '.jpeg', '.bmp', '.png', '.gif',
        '.txt', '.pdf'
    );
    $max_filesize = 524288; // 0.5MB

    $file_name = $_FILES["userfile"]["name"];
    $file_ext = strtolower( substr( $file_name, strpos( $file_name, '.' ), strlen( $file_name ) - 1 ) );

    // Verify file type
    if ( !in_array( $file_ext , $allowed_ftypes ) ) {
        return 'The file you attempted to upload is not allowed.';
    }
    // Verify filesize
    if ( !filesize( $_FILES["userfile"]["tmp_name"] ) > $max_filesize ) {
        return 'The file you attempted to upload is too large.';
    }
    // Verify writing permissions
    if ( !is_writable( $upload_path ) ) {
        return 'You cannot upload the file to the specified directory.';
    }

    $full_path = $upload_path . '/' . $file_name;

    if ( move_uploaded_file( $_FILES["userfile"]["tmp_name"], $full_path ) ) {
        // Get simplified file type
        $type = 'text';
        if (array_search($file_ext, $allowed_ftypes) < 5) {
            $type = 'image';
        }

        // Add to meta
        $file_array = get_user_meta($current_user_id , 'uploaded_files', true);
        $file_array_add = array(
            'path' => $full_path,
            'name' => $file_name,
            'type' => $type
        );

        // Check file existence
        $file_exists = false;
        for ($i = 0; $i < count($file_array); $i++) {
            $at_i = $file_array[$i];
            if ($at_i['name'] === $file_name) {
                $file_exists = true;
            }
        }

        if (!$file_exists) {
            $file_array[] = $file_array_add;
            update_user_meta($current_user_id , 'uploaded_files', $file_array);
        }

        // Complete
        return 'File uploaded successfuly, view it <a href="' . $full_path . '" title="Your File">here</a>.';
    } else {
            return 'There was an error uploading the file. Please, try again.';
        }
}

if (!empty($_POST)) {
    my_contact_form_generate_response("success", check_form());
}
?>

<style type="text/css">
  .error{
    padding: 5px 9px;
    border: 1px solid red;
    color: red;
    border-radius: 3px;
  }

  .success{
    padding: 5px 9px;
    border: 1px solid green;
    color: green;
    border-radius: 3px;
  }

  form span{
    color: red;
  }
</style>

<div id="respond">
  <?php echo $response; ?>
         <form action="<?php the_permalink(); ?>" method="post" enctype="multipart/form-data">
            <p>
                <label for="file">Select a file:</label>
                <input type="file" name="userfile" id="file"> <br />
                <input type="hidden" name="submitted" value="1">
                <input type="submit" value="upload">
            <p>
        </form>
</div>

<div id="my-files">
    <?php
        $current_user = wp_get_current_user();
        if ( !($current_user instanceof WP_User) ) {
            echo '<p>Undefined user</p>';
        } else {
            $file_array = get_user_meta($current_user->ID, 'uploaded_files', true);
            // File array is empty
            if ($file_array === '') {
                echo '<p>No files</p>';
            } else {
                echo '<h3>Files:</h3><br/>';
                $len = count($file_array);
                for ($i = 0; $i < $len; $i++) {
                    $file_data = $file_array[$i];
                    $f_path = $file_data['path'];
                    $f_name = $file_data['name'];
                    $f_type = $file_data['type'];
                    if ($f_type === 'image') {
                        echo '<img src="' . $f_path . '">Can not get image: ' . $f_name . '</img>';
                    } else {
                        echo '<a href=' . $f_path . '>' . $f_name . ' -> ' . $f_type . '</a><br/>';
                    }
                }
            }
        }
    ?>
</div>

编辑:

这是我更新的.htaccess文件:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /~mdg17761/druskamdg.com/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/?uploads/
RewriteRule . /~mdg17761/druskamdg.com/index.php [L]
</IfModule>

# END WordPress

旧的没有:

RewriteCond %{REQUEST_URI} !^/?uploads/

问题仍然存在,但我发布了.htaccess文件,因为有多个wordpress安装,所以上面的RewriteCond可能需要有点不同。我尝试在几个方面混合使用/~mdg17761/druskamdg.com/,但我没有让它工作。

1 个答案:

答案 0 :(得分:0)

如果我正确理解您的问题,文件都已成功上传,但是通过浏览器访问文件是个问题。听起来你正在遇到Word重写Word使用的问题。重写从安装的根级别开始,并转移到子目录,除非另有说明。

在Wordpress安装的根目录中,查看你的.htaccess,并添加一行告诉它在重写时忽略子目录(在这种情况下,&#39; / uploads&#39;)

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/?uploads/
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
相关问题