[英]PHP $_FILES[“fileToUpload”][“tmp_name”]
How can I turn $_FILES["fileToUpload"]["tmp_name"]
into a variable to be used in a move_uploaded_file? 如何将
$_FILES["fileToUpload"]["tmp_name"]
转换为在move_uploaded_file中使用的变量?
This works: 这有效:
$filename = compress_image(($_FILES["fileToUpload"]["tmp_name"]), $url, 30);
But I am trying to get something like this: 但是我试图得到这样的东西:
$filename = compress_image($images, $url, 30);
But, when do above it does not work. 但是,当执行上述操作时不起作用。
One alternative I was starting on was: 我开始的另一种选择是:
file_put_contents($target_file , $image);
In that case, the image was named into directory properly, but the image was always broken. 在这种情况下,该映像已正确命名到目录中,但该映像始终损坏。
To clarify: 澄清:
Need to turn ($_FILES["fileToUpload"]["tmp_name"]) into a variable that is result of 需要将($ _FILES [“ fileToUpload”] [“ tmp_name”])转换为以下结果的变量:
ob_start(); echo imagejpeg($image,null,30);
$image =ob_get_clean();
ob_end_clean();
$image = addslashes($image);
I need to use $image to save to directory. 我需要使用$ image保存到目录。 $image has been successfully stored into mysql.
$ image已成功存储到mysql中。 I have tried encode, and decode on $image, still no luck.
我已经尝试对$ image进行编码和解码,但是还是没有运气。
Let me explain the problem step-by-step: 让我逐步解释问题:
The mess starts at this line: 混乱始于这一行:
$image = imagecreatefromstring(file_get_contents($_FILES['fileToUpload']['tmp_name']));
Problems: 问题:
move_uploaded_file()
first move_uploaded_file()
将上传的文件移动到永久位置 file_get_contents()
is unnecessary; file_get_contents()
; you can use imagecreatefromjpeg()
(and other formats, like GIF, PNG, etc) to replace the imagecreatefromstring(file_get_contents())
things imagecreatefromjpeg()
(以及其他格式,例如GIF,PNG等)替换imagecreatefromstring(file_get_contents())
内容 Here is your compress_image()
function: 这是您的
compress_image()
函数:
function compress_image($source_url, $destination_url, $quality) {
$info = getimagesize($source_url);
if ($info['mime'] == 'image/jpeg') {
$image = imagecreatefromjpeg($source_url);
} elseif ($info['mime'] == 'image/gif') {
$image = imagecreatefromgif($source_url);
} elseif ($info['mime'] == 'image/png') {
$image = imagecreatefrompng($source_url);
}
imagejpeg($image, $destination_url, $quality);
return $destination_url;
}
Problems: 问题:
imagejpeg()
will fail (but you didn't cater it; always check the return value of function) imagejpeg()
将会失败(但您并未满足要求;请始终检查函数的返回值) $destination_url
is writable & exist or not $destination_url
是否可写且存在或不存在 Next, assumes the compress_image()
works well and returns $destination_url
with a valid JPEG created at the path, the following codes cause further problems: 接下来,假设
compress_image()
运作良好,并返回$destination_url
并在路径上创建了有效的JPEG,以下代码会引起进一步的问题:
$sql = "INSERT INTO fffdf (`user_id`,`imit`) VALUES ('9','$image')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
Problems: 问题:
$image
directly into DB? $image
直接保存到DB中? It's a very bad practice to store image data in DB. $image
is inaccessible here; $image
在这里无法访问; the scope of the $image
stays in the function compress_image()
, thus $image
still contains the value before compress_image()
, unless you use global $image;
$image
的范围保留在compress_image()
函数中,因此$image
仍包含compress_image()
之前的值,除非您使用global $image;
in the compress function (which is not suggested). 在compress函数中(不建议使用)。 Pass the
$image
as function parameters by reference: 通过引用将
$image
作为函数参数传递:
function compress_image(&$image, $destination_url, $quality) 函数compress_image(&$ image,$ destination_url,$ quality)
you don't really need the $source_url
if you have the image data stored in $image
. 如果您将图像数据存储在
$image
则实际上不需要$source_url
。
Hope the above helps. 希望以上内容对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.