简体   繁体   English

PHP $ _FILES数组操作

[英]PHP $_FILES Array Manipulation

I am using a form to upload an image. 我正在使用表单上传图片。 Upon image upload, we will be able to see the uploaded image. 上传图片后,我们将能够看到上传的图片。 Then I used JCrop ( http://deepliquid.com/content/Jcrop.html ) to allow cropping for this image. 然后我使用JCrophttp://deepliquid.com/content/Jcrop.html )来允许裁剪此图像。 Let's assume I only care about JPEG images. 我们假设我只关心JPEG图像。 I am then ready to submit the form. 然后我准备提交表格。

Upon form submission I will perform some image manipulation and crop the image. 在表单提交后,我将执行一些图像处理并裁剪图像。 However I want to put the information for this cropped image back into the $_FILES array (this is a must). 但是我想把这个裁剪图像的信息放回到$_FILES数组中(这是必须的)。 How would I go about manipulating this $_FILES array in a PHP script? 我如何在PHP脚本中操作这个$_FILES数组?

Here is what I had attempted and it would not work. 这是我尝试过的,但是不行。

$upload_dir = '/Users/user/Sites/tmp/';

$file_name = $_FILES['image']['name'];
$file_name = "cropped_".$file_name;
$tmp_name = $_FILES['image']['tmp_name'];
$file_size = $_FILES['image']['size'];

$src_file = imagecreatefromjpeg($tmp_name);

list($width,$height) = getimagesize($tmp_name);

// Creates cropped image
$tmp = imagecreatetruecolor($_POST['w'], $_POST['h']);
imagecopyresampled($tmp, $src_file, 0, 0, $_POST['x'], $_POST['y'], $_POST['w'], $_POST['h'], $_POST['w'], $_POST['h']);
$small_pic_file_path = $upload_dir.$file_name;

imagejpeg($tmp,$small_pic_file_path,85);
$message = "<img src='http://localhost/~user/tmp/".$file_name."'>";

$_FILES['image']['name'] = $file_name;
$_FILES['image']['type'] = "image/jpeg";
// unlink($tmp_name);
// if(!move_uploaded_file($upload_dir.$file_name, $tmp_name)) echo "Failure Moving Image";
$_FILES['image']['tmp_name'] = $upload_dir.$file_name;
$_FILES['image']['error'] = 0;
$sizes = getimagesize($upload_dir.$file_name);
$_FILES['image']['size'] = ($sizes['0'] * $sizes['1']);

It is allowable to change this $_FILES array? 允许更改此$_FILES数组?

Only changing in the global array won't work. 只有在全局数组中更改才能起作用。 You are just saying $_FILES['image']['tmp_name'] will be the newly created one. 你只是说$_FILES['image']['tmp_name']将是新创建的。 But the tmp location is having same old file. 但是tmp位置具有相同的旧文件。

But you have to update the image in /tmp folder. 但您必须更新/tmp文件夹中的图像。

Get your tmp path and copy the image there. 获取您的tmp路径并将图像copy到那里。

kind of copy($newImage, /tmp) - get your tmp folder path from php.ini 一种copy($newImage, /tmp) - 从php.ini获取你的tmp文件夹路径

改变一个超级全局并不是一个好主意,你最好将它复制到另一个变量中,然后做任何你想做的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM