gpt4 book ai didi

php - 多次上传和调整 class.upload.php

转载 作者:行者123 更新时间:2023-12-02 01:09:48 31 4
gpt4 key购买 nike

我正在尝试将多张图片上传到服务器,并为每张图片制作不同分辨率的版本。为此,我第一次使用 class.upload.php。 http://www.verot.net/php_class_upload.htm

我查看文档并从演示示例开始 http://www.verot.net/php_class_upload_download_zip.htm

我做了一个多输入的表单

<form name="form3" enctype="multipart/form-data" method="post" action="upload.php">
<p><input type="file" size="32" name="my_field[]" value="" /></p>
<p><input type="file" size="32" name="my_field[]" value="" /></p>
<p><input type="file" size="32" name="my_field[]" value="" /></p>
<p><input type="file" size="32" name="my_field[]" value="" /></p>
<p><input type="file" size="32" name="my_field[]" value="" /></p>
<p class="button"><input type="hidden" name="action" value="multiple" />
<input type="submit" name="Submit" value="upload" /></p>
</form>

示例中的原始 php 仅上传图像而不调整它们的大小:

$files = array();
foreach ($_FILES['my_field'] as $k => $l) {
foreach ($l as $i => $v) {
if (!array_key_exists($i, $files))
$files[$i] = array();
$files[$i][$k] = $v;
}
}

// now we can loop through $files, and feed each element to the class
foreach ($files as $file) {

// we instanciate the class for each element of $file
$handle = new Upload($file);

// then we check if the file has been uploaded properly
// in its *temporary* location in the server (often, it is /tmp)
if ($handle->uploaded) {

// now, we start the upload 'process'. That is, to copy the uploaded file
// from its temporary location to the wanted location
// It could be something like $handle->Process('/home/www/my_uploads/');
$handle->Process($dir_dest);

// we check if everything went OK
if ($handle->processed) {
// everything was fine !
echo 'ok';
} else {
// one error occured
echo ' Error: ' . $handle->error . '';
}

} else {
// if we're here, the upload file failed for some reasons
// i.e. the server didn't receive the file
echo ' Error: ' . $handle->error . '';
}
}

我想做的是处理 if ($handle->processed) {} 中的每个文件所以我采用了调整 img 大小并将其粘贴到 if ($handle->processed) {} 部分的示例中的函数。现在它看起来像这样:

if ($handle->uploaded) {

// now, we start the upload 'process'. That is, to copy the uploaded file
// from its temporary location to the wanted location
// It could be something like $handle->Process('/home/www/my_uploads/');
// now, we start a serie of processes, with different parameters
// we use a little function TestProcess() to avoid repeting the same code too many times
function TestProcess(&$handle, $title) {
global $dir_pics, $dir_dest;

$handle->Process($dir_dest);

// we check if everything went OK
if ($handle->processed) {
// everything was fine !
echo 'ok';
} else {
// one error occured
echo ' Error: ' . $handle->error . '';
}
}
if (!file_exists($dir_dest)) mkdir($dir_dest);

// ----------- save the uploaded img adding _xl to the name
$handle->file_name_body_add = '_xl';
$handle->file_overwrite = true;
TestProcess($handle, 'File originale', '');

// ----------- save the uploaded img adding _l to the name and downsizing it
$handle->file_name_body_add = '_l';
$handle->image_resize = true;
$handle->image_ratio_y = true;
$handle->image_x = 1024;
$handle->file_overwrite = true;
TestProcess($handle, 'Ridimensionato a 1024px');
}

此时脚本仅适用于第一个 img。它不会使“foreach ($files as $file)”在 $files 数组中...你能帮我找到错误在哪里吗?泰克斯丹妮尔

最佳答案

此处类的创建者...您需要先更改 $files 数组,如下所示。它在 FAQ 中:

$files = array();
foreach ($_FILES['my_field'] as $k => $l) {
foreach ($l as $i => $v) {
if (!array_key_exists($i, $files))
$files[$i] = array();
$files[$i][$k] = $v;
}
}

关于php - 多次上传和调整 class.upload.php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18702369/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com