Jump to content

small problem upload script


arbitter

Recommended Posts

<?php
$images = array('image/gif','image/jpeg','image/jpg','image/pjpeg');

if( in_array($_FILES['file']['type'],$images) && $_FILES['file']['size'] <= 10000000 )
{
    if( $_FILES['file']['error'] > 0 )
    {
        echo 'Return Code: ',$_FILES['file']['error'],'</br >';
    }
    else
    {
        echo 'Upload: ',$_FILES['file']['name'],'<br />';
        echo 'Type: ',$_FILES['file']['type'],'<br />';
        echo 'Size: ',($_FILES['file']['size'] / 1024),'KB</br >';
        echo 'Temp File: ',$_FILES['file']['tmp_name'],'<br />';

        $uploaddir = date("F")." '".date("y");

        if( file_exists('uploads/'. $uploaddir . '/' . $_FILES['file']['name']) )
        {
		function findexts ($_FILES) 
		{ 
			$filename = strtolower($_FILES) ; 
			$exts = split("[/\\.]", $filename) ; 
			$n = count($exts)-1; 
			$exts = $exts[$n]; 
			return $exts; 
		} 
		$ext = findexts ($_FILES['file']['tmp_name']) ; 
		$ran = date(dmY);
		$target= "uploads/";
			if(file_exists($target . $uploaddir))
			{	$target2 = $target . $uploaddir . "/" . $_FILES['file']['name']; 
			}
			else
			{	mkdir($target . $uploaddir);
				$target2 = $target . $uploaddir . "/" . $_FILES['file']['name'];
			}

		$target3 = $target2 . $ran . $ext;
		move_uploaded_file($_FILES['file']['tmp_name'], $target3); 

        }
        else
        {	if(file_exists("uploads/" . $uploaddir))
			{	$target="uploads/" . $uploaddir; 
			}
			else
			{	mkdir("uploads/" . $uploaddir);
				$target="uploads/" . $uploaddir;
			}
		move_uploaded_file($_FILES['file']['tmp_name'],'uploads/'. $uploaddir . '/' . $_FILES['file']['name']); 
            

        }
    }
}
else
{
    echo 'Ongeldig bestand.';
echo 'Kijk grootte en type na.';
} 
?>

 

Uploading a file that doesn't exist yet works perfect, but uploading a file that already exists does not work... I get:

Fatal error: Cannot use string offset as an array in C:\xampp\htdocs\Depypere\uploader.php on line 34

 

line 34 contains "$target2 = $target . $uploaddir . "/" . $_FILES['file']['name']; "

I don't understand why it doesn't work.. I've checked everyhting 10 times, without exaggerating

Link to comment
Share on other sites

Try:

 

if(file_exists($target . $uploaddir))
            {   var_dump($target, $uploaddir, $_FILES['file']['name']);
$target2 = $target . $uploaddir . "/" . $_FILES['file']['name']; 
            }

 

function findexts ($_FILES) 
         { 
            $filename = strtolower($_FILES) ; 
            $exts = split("[/\\.]", $filename) ; 
            $n = count($exts)-1; 
            $exts = $exts[$n]; 
            return $exts; 
         } 
         $ext = findexts ($_FILES['file']['tmp_name']) ;

 

Is the same as

 

$ext = pathinfo($_FILES['file']['tmp_name'], PATHINFO_EXTENSION);

Link to comment
Share on other sites

All you have to do is look at what the expected output of this code is.

 

		$ext = findexts ($_FILES['file']['tmp_name']) ; 
		$ran = date(dmY);
		$target= "uploads/";
			if(file_exists($target . $uploaddir))
			{	$target2 = $target . $uploaddir . "/" . $_FILES['file']['name']; 
			}
			else
			{	mkdir($target . $uploaddir);
				$target2 = $target . $uploaddir . "/" . $_FILES['file']['name'];
			}

		$target3 = $target2 . $ran . $ext;

 

It's going to be the original file name with the day month and year at the end. So that means that if you wanted to upload a file with the same name, you're going to have to wait till tomorrow. All you have to do to fix this is change the date at the end to microtime like this.

 

$ran = intval(microtime(TRUE));

 

Now you can upload a file as long as someone else isn't uploading a file with the same filename at the exact microsecond that you are, but what are the odds of that happening any time soon?

 

Also you should think about taking the contents of the file and sticking it in a database. It saves on lines of code, it's more secure, and i'm pretty sure its faster to read.

Link to comment
Share on other sites

But if you do that, you don't move the uploaded file do you...

And indeed, the file does not get uploaded at all, I get:

 

Upload: JorenFuifRordic.jpg

Type: image/jpeg

Size: 39.7275390625KB

string(8) "uploads/" string(11) "January '10" string(19) "JorenFuifRordic.jpg" uploads/January '10/JorenFuifRordic.jpg

uploads/January '10/JorenFuifRordic.jpg1262725411tmp

 

but no file, not even the temp file, gets uploaded...

Link to comment
Share on other sites

But if you do that, you don't move the uploaded file do you...

And indeed, the file does not get uploaded at all, I get:

 

Upload: JorenFuifRordic.jpg

Type: image/jpeg

Size: 39.7275390625KB

string(8) "uploads/" string(11) "January '10" string(19) "JorenFuifRordic.jpg" uploads/January '10/JorenFuifRordic.jpg

uploads/January '10/JorenFuifRordic.jpg1262725411tmp

 

but no file, not even the temp file, gets uploaded...

 

That's what we want, why do we need to actually have to upload the file when we're just debugging. I've spotted quite a few discrepancies in your code. So what I did was I went ahead and slimmed it down a bit getting rid of unnecessary crap. This should meet your needs.

 

<?php
$images = array('image/gif','image/jpeg','image/jpg','image/pjpeg');
if(in_array($_FILES['file']['type'],$images) && $_FILES['file']['size'] <= 10000000 ) {
	if( $_FILES['file']['error'] > 0 ) {
		echo 'Return Code: ' . $_FILES['file']['error'] . '<br / >';
	}
	else {
		echo 'Upload: ',$_FILES['file']['name'],'<br />';
		echo 'Type: ',$_FILES['file']['type'],'<br />';
		echo 'Size: ',($_FILES['file']['size'] / 1024),'KB</br >';
		echo 'Temp File: ',$_FILES['file']['tmp_name'],'<br />';
		$upload_dir = 'uploads/' . date("F") . " " . date("y") . '/';
		$upload_file = pathinfo($_FILES['file']['name'],PATHINFO_FILENAME);
		$upload_file .= '-' . intval(microtime(TRUE));
		$upload_file .= pathinfo($_FILES['file']['name'],PATHINFO_EXTENSION);
		if(!file_exists($upload_dir)) {
			mkdir($upload_dir);
		}
		$target = $upload_dir . $upload_file; 
		move_uploaded_file($_FILES['file']['tmp_name'],$target); 
	}
}
else {
	echo 'Ongeldig bestand.';
	echo 'Kijk grootte en type na.';
} 
?>

Link to comment
Share on other sites

Ohboy, I have a lot to learn.

One problem; the file that get's uploaded is 'JorenFuifRordic-1262780889jpg', so there still needs to be a dot added. So I've added one line here:

$upload_file = pathinfo($_FILES['file']['name'],PATHINFO_FILENAME);
		$upload_file .= '-' . intval(microtime(TRUE));
		$upload_file .= '.';
		$upload_file .= pathinfo($_FILES['file']['name'],PATHINFO_EXTENSION);

 

Thanks a lot for the help, works now! :)

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.