Jump to content

Image Uploader Has Stopped Working


stualk

Recommended Posts

I pinched the code below from and php scripts site. It's always worked fine but since I moved my site over to a new server with the latest version of PHP it's suddenly stopped working. Can anyone understand why that might be? I've turned register_globals on so it can't be that issue. Here's the error i'm getting and the code is below:

"Fatal error: Cannot redeclare scandir() in myurlhere.com/upload_images.php on line 56"

[code]
<?php

$dir = "myurlgoeshere.com/images";

$types = array("image/pjpeg", "image/gif");
   
//Function to do a directory listing
function scandir($dirstr) {
echo "<pre>\n";
passthru("ls -l -F $dirstr 2>&1 ");
echo "</pre>\n";
}

//Check to determine if the submit button has been pressed
if((isset($_POST['submit'])) and ($_POST['PW'] == $pw)){

//Shorten Variables
$tmp_name = $_FILES['upload']['tmp_name'];
$new_name = $_FILES['upload']['name'];
$path = $_POST['subdir'];
$fullpath = "$dir$path/";
$fullpath = str_replace("..", "", str_replace("\.", "", str_replace("//", "/", $fullpath)));
$clean_name = ereg_replace("[^a-z0-9._]", "", str_replace(" ", "_", str_replace("%20", "_", strtolower($new_name) ) ) );

// lets see if we are uploading a file or doing a dir listing
if(isset($_POST['Dir'])){
echo "Directory listing for $fullpath\n";
scandir("$fullpath");
}else{


//Check MIME Type
if ((in_array($_FILES['upload']['type'], $types)) and (!file_exists($fullpath.$clean_name))){

// create a sub-directory if required
if (!is_dir($fullpath)){
mkdir("$fullpath", 0755);
}
//Move file from tmp dir to new location
move_uploaded_file($tmp_name,$fullpath . $clean_name);

echo "$clean_name of {$_FILES['upload']['size']} bytes was uploaded sucessfully to $fullpath";

}else{
         
//Print Error Message
echo "File <strong><em>{$_FILES['upload']['name']}</em></strong> Was Not Uploaded - bad file type or file already exists<br />";
//Debug
$name =  $_FILES['upload']['name'];
$type =    $_FILES['upload']['type'];
$size =    $_FILES['upload']['size'];
$tmp =    $_FILES['upload']['name'];
   
echo "Name: $name<br />Type: $type<br />Size: $size<br />Tmp: $tmp";

}
     
}
} else {
echo 'Ready to upload your file';
} ?>
[/code]
Link to comment
Share on other sites

JJohnsen, as the $fullpath var is enclosed by double quotes, it should work fine - although unnecessary.

taking a guess, you might not have sufficient priveliges set up in your php.ini file that allows running of command line tools:

[code]
        passthru("ls -l -F $dirstr 2>&1 ");
[/code]

it maybe worth looking at some of PHP's directory/file handling functions to put together a suitable replacement, or changing the setting (the safe_mode ones i think - take a look at [url=http://uk2.php.net/passthru]the manual for passthru[/url]) in your php.ini file to allow access to command line tools like 'ls'

cheers
Link to comment
Share on other sites

Ok, thanks for that. As i'm a bit of a novice and so to avoid me making a stupid mistake, can you tell me roughly what i'm looking for in the php.ini file? I've had a scan through and couldn't find anything to do with command line tools. The links you gave me mentions using the passthru() function in place of exec() or system(). Is that in the php.ini file?

I'm confused!

By the way, I did try JJohnsen's method and that did stop me from getting the error I was getting previously. The only problem is it then started to give me an error saying "No such file or directory" but the path was correct and the directory does exist, so I was a little confused to say the least! Doesn't take much like...!
Link to comment
Share on other sites

I'm not sure I need to change any settings in the php.ini file. I made the changes as suggested by JJohnsen above and the upload window half works now. It no longer gives me the error I was getting but when I try to do a directory listing or upload a file I get this error 3 times:

Warning: scandir() [function.scandir]: (errno 2): No such file or directory

Not too sure why because i've checked and double checked and the directory/path is fine.
Link to comment
Share on other sites

after this line:
[code]
$fullpath = str_replace("..", "", str_replace("\.", "", str_replace("//", "/", $fullpath)));
[/code]
just put an: echo $fullpath; to display the path, and then double check that the path displayed exists. you could be getting into a bit of a mix with relative/absolute paths.
Link to comment
Share on other sites

That helped! It turns out the path to the correct folder is slightly different on the new server and I only found out when I displayed the path that was being used. Typical but thanks!

The upload is working fine now but there's an option to do a directory listing to see what files have been uploaded to the folder and instead of using the folder that i've specified it's doing a directory listing for the folder that the upload window is in and showing all the database.php pages, etc.

Any idea what might be causing that?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.