note 45448 added to function.getimagesize
| From: | ben at mekhaye dot net | Date: | Tue, 07 Sep 2004 17:34:24 +0000 |
| Subject: | note 45448 added to function.getimagesize | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-76161@lists.php.net to get a copy of this message | ||
I had some trouble getting getimagesize() to work. I kept getting the error message:
Warning: getimagesize(filenamehere): failed to open stream: No such file or directory
I realized that this page (that is, php.net's getimagesize() page) is not very specific as to
what, exactly, the "string filename" that must be passed to getimagesize()
<i>is</i>.
I had the following code, handling the uploaded file upon submit:
global $userfile;
// get rid of magic quotes and slashes and all that
$userfile = stripslashes($userfile);
if (is_uploaded_file($userfile)) {
$name = $_FILES['userfile']['name'];
$mime = $_FILES['userfile']['type'];
$size = $_FILES['userfile']['size'];
$tmp_name = $_FILES['userfile']['tmp_name'];
// Open the uploaded file
$file = fopen($userfile, "r");
// Read in the uploaded file
$fileContents = fread($file, filesize($userfile));
// HERE'S THE IMPORTANT PART:
// is the file an image?
if ($image_info = getimagesize($file)) {
$width = $image_info[0];
$height = $image_info[1];
}
This produced the afforementioned error:
Warning: getimagesize: failed to open stream: No such file or directory
So I tried passing different things to getimagesize: $file, $userfile, stripslashes($userfile),
$name (set to $_FILES['userfile']['name'] ), $tmp_name (set to
$_FILES['userfile']['tmp_name'] ).
$tmp_name (set to $_FILES['userfile']['tmp_name'] ) worked like a charm; none of
the others worked. It wasn't obvious to me that
$_FILES['userfile']['tmp_name'] was what I'd need to pass to
getimagesize().
----
Manual Page -- http://www.php.net/manual/en/function.getimagesize.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+45448
Delete -- http://master.php.net/manage/user-notes.php?action=delete+45448&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+45448&report=yes
Search -- http://master.php.net/manage/user-notes.php