Error in reading from a directory

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

Error in reading from a directory

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Single quotes problem? Not using isset()? Too much hot sauce?
richmix
Forum Commoner
Posts: 31
Joined: Fri Dec 22, 2006 5:21 am

Post 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.
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

Post by kpraman »

changed to
$_FILES["$fileName"]["tmp_name"];

also tried,
echo $_FILES["$fileName"]['tmp_name'];

still not working.
brendandonhue
Forum Commoner
Posts: 71
Joined: Mon Sep 25, 2006 3:21 pm

Post by brendandonhue »

$_FILES is for handling file uploads, but it doesn't look like that's what you're doing.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post 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);
}
?>
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

Post 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?
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

Use the code snippet I posted combined with mysql functions and SQL queries.

I'm not going to write it for you. =;
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

Post by kpraman »

I have allready written the code for inserting filename, i want the $filetmp_name.
brendandonhue
Forum Commoner
Posts: 71
Joined: Mon Sep 25, 2006 3:21 pm

Post by brendandonhue »

There is no $filetmp_name. $_FILES array is for file uploads.
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

Post by kpraman »

what is the alternate solution to solve this?
brendandonhue
Forum Commoner
Posts: 71
Joined: Mon Sep 25, 2006 3:21 pm

Post 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.
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post 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>";

?>
Post Reply