【问题标题】:Wordpress copy to uploads directory creates 0 bytes filesWordpress 复制到上传目录创建 0 字节文件
【发布时间】:2014-02-03 03:40:43
【问题描述】:

出于某种原因,我从一开始就使用的这段代码似乎已停止在我的开发环境中工作,该开发环境是在免费托管服务上设置的。但是,它仍然可以在本地工作。现在,我不记得我是否曾经在开发环境中实际测试过它,所以也许它从一开始就没有在那里工作过。

基本上,我使用 Gravity Forms 来创建自定义帖子(工作得很好),并且还使用多个文件上传来将图像附件添加到帖子中。由于 GF 并不完全支持它,但我摆弄了一下并设法在这里创建了这个小功能:

function create_post_attachment_from_gf($f, $from_url) {
    //for image field, it sends content with a bunch of other parameters in a string with pipe delimiters
    $split_image = explode('|', $from_url);
    if( count($split_image) > 0 )
        $from_url = $split_image[0];

    if( !empty( $_FILES[$f]['name'] ) ) {
        $file = $_FILES[$f];

        $uploads = wp_upload_dir( null );
        $filename = wp_unique_filename( $uploads['path'], $file['name'], null );
        // Strip the query strings.
        $filename = str_replace('?','-', $filename);
        $filename = str_replace('&','-', $filename);

        // Compute the URL
        $url = $uploads['url'] . "/$filename";

        $new_file = $uploads['path'] . "/$filename";

        copy($from_url,$new_file);

        // Move the file to the uploads dir
        $stat = stat( dirname( $new_file ));
        $perms = $stat['mode'] & 0000666;
        @ chmod( $new_file, $perms );


        return array( 'file' => $new_file, 'url' => $url, 'type' => $file['type'], 'name' => $file['name'] );
    }

    return false;
}

然后这样调用它来创建附件:

$copied_file = create_post_attachment_from_gf('input_' . $file_uploads[$i], $entry[$file_uploads[$i]]);
    $subdir = wp_upload_dir( null )["subdir"];
        if( $copied_file ){
            $attachment = array(
                'guid' => $copied_file['url'], 
                'post_mime_type' => $copied_file['type'],
                'post_title' => $copied_file['name'],
                'post_content' => '',
                'post_status' => 'inherit'
            );


            $attach_id = wp_insert_attachment( $attachment, $subdir . '/' .$copied_file['name'], $post_id );

            $attach_data = wp_generate_attachment_metadata( $attach_id, $copied_file['file'] );
            wp_update_attachment_metadata( $attach_id,  $attach_data );

        }

看起来副本在这里失败了。它在我的上传文件夹中创建一个具有正确文件名的空文件。我检查了我的文件夹权限,甚至继续将上传下的所有内容(包括重力表单文件夹)设置为 777,但由于某种原因它仍然无法正常工作。

我的本​​地和远程网站都在 WP 3.8.1 上运行,并且所有插件都是最新的。除了一些不应干扰的测试数据外,数据库几乎完全相同。直接从仪表板添加附件可以正常工作。这是我应该与托管公司接洽的事情吗?还是我的代码有问题?或者也许有一种方法可以在不移动文件的情况下创建附件——这对我来说很好。

感谢任何帮助。

【问题讨论】:

  • 它不起作用,因为它是编造的。以第 3 行为例。您正在从包含 URL 的字符串创建一个数组,每个数组元素都是 URL 的一部分,由管道 | 分隔。特点。为什么? URL 有斜杠 / 而不是管道。每一行都是这样,随机的。这会让刚开始的人感到困惑。
  • 管道分隔的字符串是由重力形式生成的。我没有弥补。再一次,该代码在我的本地测试环境中运行良好。我调试了路径,它们都井然有序,所以我的问题可能出在服务器权限的某个地方。

标签: php wordpress copy


【解决方案1】:

嗯,看起来,虽然复制仍然无法远程工作,但我本可以从一开始就将图像直接上传到正确的文件夹。

//Have gform upload directly in the upload folder
    add_filter("gform_upload_path", "change_upload_path", 10, 2);
    function change_upload_path($path_info, $form_id){
        $uploads = wp_upload_dir( null );

        $path_info["path"] = $uploads['path'] . "/";
        $path_info["url"] = $uploads['url'] . "/";

        return $path_info;
    }

所以我删除了移动文件的部分,并从我以前的函数中重新创建了文件名,现在一切都按计划进行。不敢相信我在这上面花了几个小时...希望这对其他人有所帮助?

【讨论】:

    猜你喜欢
    • 2015-09-17
    • 1970-01-01
    • 2018-01-12
    • 2012-09-23
    • 2011-03-10
    • 1970-01-01
    • 2021-05-25
    • 2021-11-27
    • 1970-01-01
    相关资源
    最近更新 更多