在php中优化图像时找不到这样的文件或目录(No such file or directory found while optimize the image in php)
我正在上传图像和我正在优化的图像,但我收到错误“没有这样的文件或目录”。 我收到以下代码的错误。
$source_img =basename($_FILES["fileToUpload"]["name"]);
如果我写
$source_img = 'assets/img/login-bg.jpg';
然后它正在工作。 我想上传图片然后优化它。我收到了错误
Warning: getimagesize(demoimg.JPG): failed to open stream: No such file or directory in C:\xampp\htdocs\... on line 6 Notice: Undefined variable: image in C:\xampp\htdocs\... on line 17 Warning: imagejpeg() expects parameter 1 to be resource, null given in C:\xampp\htdocs\... on line 17
HTML
<form action="" method="post" enctype="multipart/form-data"> Select image to upload: <input type="file" name="fileToUpload" id="fileToUpload"> <input type="submit" value="Upload Image" name="submit"> </form>
PHP
function compress($source, $destination, $quality) { $info = getimagesize($source); if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($source); elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($source); elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($source); imagejpeg($image, $destination, $quality); return $destination; } if(isset($_POST['submit'])){ $source_img =basename($_FILES["fileToUpload"]["name"]); $temp = explode(".", $source_img); $newfilename = round(microtime(true)) . '.' . end($temp); $destination_img= "assets/$newfilename"; $d = compress($source_img, $destination_img, 90); }
I am uploading the image and that image I am optimizing but I am getting the error "No such file or directory". I am getting the error on below code.
$source_img =basename($_FILES["fileToUpload"]["name"]);
If I write
$source_img = 'assets/img/login-bg.jpg';
then it is working. I want to upload image and then optimize it.I am getting the error
Warning: getimagesize(demoimg.JPG): failed to open stream: No such file or directory in C:\xampp\htdocs\... on line 6 Notice: Undefined variable: image in C:\xampp\htdocs\... on line 17 Warning: imagejpeg() expects parameter 1 to be resource, null given in C:\xampp\htdocs\... on line 17
HTML
<form action="" method="post" enctype="multipart/form-data"> Select image to upload: <input type="file" name="fileToUpload" id="fileToUpload"> <input type="submit" value="Upload Image" name="submit"> </form>
php
function compress($source, $destination, $quality) { $info = getimagesize($source); if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($source); elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($source); elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($source); imagejpeg($image, $destination, $quality); return $destination; } if(isset($_POST['submit'])){ $source_img =basename($_FILES["fileToUpload"]["name"]); $temp = explode(".", $source_img); $newfilename = round(microtime(true)) . '.' . end($temp); $destination_img= "assets/$newfilename"; $d = compress($source_img, $destination_img, 90); }
原文:https://stackoverflow.com/questions/43703188
更新时间:2022-06-20 09:06
最满意答案
$str = "this.key.exists"; $p = &$array; // point to array root $exists = true; foreach(explode('.', $str) as $step) { if (isset($p[$step])) $p = &$p[$step]; // if exists go to next level else { $exists = false; break; } // no such key } if($exists) $p = "new value";
$str = "this.key.exists"; $p = &$array; // point to array root $exists = true; foreach(explode('.', $str) as $step) { if (isset($p[$step])) $p = &$p[$step]; // if exists go to next level else { $exists = false; break; } // no such key } if($exists) $p = "new value";
相关问答
更多-
$str = "this.key.exists"; $p = &$array; // point to array root $exists = true; foreach(explode('.', $str) as $step) { if (isset($p[$step])) $p = &$p[$step]; // if exists go to next level else { $exists = false; brea ...
-
如何在php中查找非空值并显示if条件[复制](How to find the not null values in php and display in if condition [duplicate])[2022-05-11]
如果你不想为空而不是null数组,请尝试像这样 '','b' => 'Kani' , 'c' => '', 'd' => 'Raja'))); ?> 点击这里: https : //eval.in/737817 If you want not empty not null array try like this ... -
你只需要使用第二个参数设置为TRUE json_decode() 。 $str = '...'; $json = json_decode($str, TRUE); $artist = $json['response']['artists']; //$artist = json_decode($str, TRUE)['response']['artists']; as of PHP 5.4 print_r($artist); 输出: Array ( [0] => Array ...
-
在关键点的Smarty数组(Smarty array with dot in key)[2023-07-27]
我在这里寻找自己的答案后,“意外地”找到了这个问题的答案。 在这种情况下,我使用主机名作为总是有点的键。 您可以使用围绕虚线键名称的{}来访问它们。 例如{$var.foo.bar.{"my.hostname.example.com"}.ipaddress} 。 这种“转义”语法也可用于需要扩展包含点的变量的情况。 例如{$var.foo.bar.{$var.bingo}} I've "accidentally" found the answer to this question after I was l ... -
while循环简单的解决方案: function firstFreeKey($array) { $i = 0; while(isset($array[$i])) $i++; return $i; } $array = array( 1 => 'A', 5 => 'B', 2 => 'C', 3 => 'D', 6 => 'E', 0 => 'F' ); echo firstFreeKey($array); 输出: 4 实时预览 Easy solution by while loop: f ...
-
array_search('green', array_reverse($array)); 先倒转,然后做搜索 array_search('green', array_reverse($array)); Reverse it first, then do your search
-
筛选数组以仅包含符合条件的项目。 $target = 80; $filtered = array_filter($array, function($x) use ($target) { return $x <= $target; }); 然后对它进行分类,除非它已经分类。 sort($filtered); 最大值<=您的搜索目标将是最后一个元素。 $value = end($filtered); $key = key($filtered); 要将此方法用于>= ,可以在array_filte ...
-
1)使用explode功能的简单解决方案: $chain = "id:20604#*#user_id:32444#*#session_id:0#*#version:142#*#modified:1438610605#*#name:recrutement#*#push:0#*#last_push_execution:0#*#allowempty:1"; $result = []; foreach (explode("#*#", $chain) as $c) { $pair = explode(":" ...
-
你必须把`围绕LIKE因为它是SQL中的保留字! 所以看起来应该是这样的: INSERT INTO `like` You have to put ` around LIKE since it's a reserved word in SQL! So it should look like this: INSERT INTO `like`
-
你可以通过array_search()函数找到数组的位置。 假设我们有一个数组。 $a = array( 'blue' => 'nice', 'car' => 'fast', 'number' => 'none' ); echo array_search("car",array_keys($a)); you can find the position of array with array_search() function. suppose we have an array. ...