4

有谁知道如何使用 PHP 上传图片并调用 UploadHandler.php?

我不确定需要传递哪些信息以及以何种格式传递。

这是我到目前为止所拥有的:

$prop="test";
session_id($prop);
@session_start();
$url = 'http://christinewilson.ca/wp-content/uploads/2013/02/port_rhdefence.png';
$file_name[] = file_get_contents($url);

error_reporting(E_ALL | E_STRICT);
require('UploadHandler.php');
$upload_handler = new UploadHandler(array(
    'user_dirs' => true
));
4

5 回答 5

4

响应包含在 UploadHandler 类对象中,可以如下所示进行检索。

$upload_handler = new UploadHandler();
$response = $upload_handler->response;
$files = $response['files'];
$file_count = count($files);
for ($c = 0; $c < $file_count; $c++) {
   if (isset($files[$c]->error))
       continue;
   $type = $files[$c]->type;
   $name = $files[$c]->name;
   $url = $files[$c]->url;
}
于 2015-01-23T06:08:25.687 回答
3

我找不到通过 php 获取文件名的方法,所以我必须自己做。

首先你需要在 UploadHandler.php 下添加一个公共变量

class UploadHandler
{
    public $file_name;
    protected $options;

然后将其添加到创建名称的函数中

protected function get_file_name($name,
        $type = null, $index = null, $content_range = null) {

    $this->file_name = $this->get_unique_filename(
        $this->trim_file_name($name, $type, $index, $content_range),
        $type,
        $index,
        $content_range
    );
    return $this->file_name;
}

然后在 index.php 下你可以做这样的事情

$upload_handler = new UploadHandler();
echo "\r\n [" . $upload_handler->fileName . "]\r\n";

我希望这可以帮助您或节省一些时间:)

于 2013-06-20T02:31:29.267 回答
2

您可以使用基本插件

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>jQuery File Upload Example</title>
</head>
<body>
<input id="fileupload" type="file" name="files[]" data-url="server/php/" multiple>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="js/vendor/jquery.ui.widget.js"></script>
<script src="js/jquery.iframe-transport.js"></script>
<script src="js/jquery.fileupload.js"></script>
<script>
$(function () {
    $('#fileupload').fileupload({
        dataType: 'json',
        done: function (e, data) {
            $.each(data.result.files, function (index, file) {
                $('<p/>').text(file.name).appendTo(document.body);
            });
        }
    });
});
</script>
</body> 

于 2013-05-15T09:38:22.790 回答
1

我遇到了同样的问题,我想在 PHP 中将 UploadHandler.php 创建的所有 URL 写入 mySQL 数据库。如果您查看代码,您会看到

public function post($print_response = true)

实际上从 generate_response 中返回数据结构(这是一个包含所有已处理图像元数据的数组,如图像大小、已清理的 url 等),但对 $this->post() 的调用从不执行任何操作。所以我添加了一个变量

protected $upload_content = [];

到类定义并更改了函数中的逻辑

protected function initialize()

        case 'POST':
             $this->upload_content = $this->post(false);
             break;

在处理完图像后更新此变量(如果您正在使用 GET 案例,则需要执行类似的操作)。然后,我在类中添加一个公共函数来获取这个变量

 public function get_upload_content() {
     return $this->upload_content;
 }

现在可以像这样调用 UploadHandler 类

 $upload_handler = new UploadHandler();
 $images = $upload_handler->get_upload_content();
 // Call a function that writes the urls in $images array to a database

希望这可以帮助!

于 2014-06-11T00:40:04.757 回答
0

首先,您应该创建受保护的变量:

protected $options;
protected $uploaded_files = [];

那么你应该在 post() 方法中为这个变量分配响应值:

$this->uploaded_files = $response;
return $this->generate_response($response, $print_response);

那么您应该创建将返回该响应的公共方法:

public function get_uploaded_files() {
        return $this->uploaded_files;
    }

最后你应该启动类并调用方法:

$uploadPicture = new UploadHandler();
        $images = $uploadPicture->get_uploaded_files();

希望这可以帮助 !

于 2019-03-08T23:10:20.907 回答