Page 1 of 1
Error in reading from a directory
Posted: Wed Dec 27, 2006 9:17 am
by kpraman
Code: Select all
$dir = "./images";
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($fileName = readdir($dh)) !== false) {
$filetmp_name=$_FILES['$fileName']['tmp_name'];//error line
$inserted=$pictures->AddPicture($fileName, $filetmp_name, $pictureDate, 'pictures');
}
closedir($dh);
}
}
I am using the above code to read imagenames from a directory and insert into database. the error line is not working.
Posted: Wed Dec 27, 2006 9:25 am
by feyd
Single quotes problem? Not using
isset()? Too much hot sauce?
Posted: Wed Dec 27, 2006 9:51 am
by richmix
feyd wrote:Single quotes problem? Not using
isset()? Too much hot sauce?
Definitely the hot sauce.
Change the single quotes to double quotes. Tah dah.
Posted: Wed Dec 27, 2006 11:19 pm
by kpraman
changed to
$_FILES["$fileName"]["tmp_name"];
also tried,
echo $_FILES["$fileName"]['tmp_name'];
still not working.
Posted: Wed Dec 27, 2006 11:33 pm
by brendandonhue
$_FILES is for handling file uploads, but it doesn't look like that's what you're doing.
Posted: Wed Dec 27, 2006 11:41 pm
by daedalus__
PHP Manual wrote:Example 2. List all files in the current directory and strip out . and ..
Code: Select all
<?php
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo "$file\n";
}
}
closedir($handle);
}
?>
Posted: Wed Dec 27, 2006 11:42 pm
by kpraman
I want to read the contents of the directory and insert the details (filename, filetmp_name) of the files to insert into database. How to do it?
Posted: Wed Dec 27, 2006 11:43 pm
by daedalus__
Use the code snippet I posted combined with mysql functions and SQL queries.
I'm not going to write it for you. =;
Posted: Wed Dec 27, 2006 11:49 pm
by kpraman
I have allready written the code for inserting filename, i want the $filetmp_name.
Posted: Thu Dec 28, 2006 12:06 am
by brendandonhue
There is no $filetmp_name. $_FILES array is for file uploads.
Posted: Thu Dec 28, 2006 12:13 am
by kpraman
what is the alternate solution to solve this?
Posted: Thu Dec 28, 2006 12:33 am
by brendandonhue
What exactly is the problem? The file doesn't have a temporary name here, so there's just nothing to put in that variable.
Posted: Thu Dec 28, 2006 12:53 am
by kpraman
Problem:
I have a class which takes filename, file_tempname. It resizes and uploads the images. I am trying to read all the contents of a directory and pass (filename, file_tempname) to that class file.
Thanks
Posted: Thu Dec 28, 2006 2:30 am
by feyd
"file_tempname" would be the existing path to the file that requires manipulation. "filename" would then likely be the path where you wish to place the newly created image.
Posted: Thu Dec 28, 2006 12:29 pm
by daedalus__
If we could see the whole script, it would help. Your code does not make any sense.
It opens a directory then rather than reading or manipulating the contents of that directory, it tries to use the path of an uploaded file that has the same name as the current file being read from the directory.
If you are uploading files then you will need to use move_uploaded_file(). Please consult the PHP manual and consider this snippet:
PHP Manual wrote:
Code: Select all
<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.
$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
?>