views:

1357

answers:

3

Can anyone tell me what is wrong with this? The file is renamed using a time stamp but the extension doesn't get extracted and placed in the new name.

    $filenameext = pathinfo($filename, PATHINFO_EXTENSION);

    $today = getdate();
    $uniqueStr = $today[year];
    $uniqueStr .= $today[mon];
    $uniqueStr .= $today[wday];
    $uniqueStr .= $today[mday];
    $uniqueStr .= $today[hours];
    $uniqueStr .= $today[minutes];
    $uniqueStr .= $today[seconds];

    $filename = $uniqueStr.".".$filenameext;

The full code:

<?php
$folder = 'images/';
$orig_w = 500;

if( isset($_POST['submit']) )
{
 $imageFile = $_FILES['image']['tmp_name'];
 $filenameext = pathinfo($filename, PATHINFO_EXTENSION);

 $today = getdate();
 $uniqueStr = $today[year];
 $uniqueStr .= $today[mon];
 $uniqueStr .= $today[wday];
 $uniqueStr .= $today[mday];
 $uniqueStr .= $today[hours];
 $uniqueStr .= $today[minutes];
 $uniqueStr .= $today[seconds];
 $filename = $uniqueStr.".".$filenameext;

 list($width, $height) = getimagesize($imageFile);

 $src = imagecreatefromjpeg($imageFile);
 $orig_h = ($height/$width)* $orig_w;

 $tmp = imagecreatetruecolor($orig_w, $orig_h);
 imagecopyresampled($tmp, $src, 0,0,0,0,$orig_w,$orig_h,$width,$height);
 imagejpeg($tmp, $folder.$filename,100);

 imagedestroy($tmp);
 imagedestroy($src);

 $filename = urlencode($filename);
 header("Location: crop.php?filename=$filename&height=$orig_h");
}

?>

A: 

While pathinfo() is supposed to return a string for single requests, it's usually defined to return an array.

Try this: $filenameext = pathinfo($path)['extension'];

However, have you logged the output of "path"? It might be the temporary path generated by your web server on upload, rather than the user's supplied file name, depending on where you get it from.

Jon Watte
pathinfo() in his call returns a string. Also, you can't do this in PHP directly: $filenameext = pathinfo("somefile.php")['extension']; : Parse error. You need to assign the return value to an array variable and then do the extraction.
Artem Russakovskii
Hhmmm this didn't work just returned a blank page. I posted the full code if that helps.
To follow up: "If options is used, this function will return a string if not all elements are requested." from http://us3.php.net/manual/en/function.pathinfo.php
Artem Russakovskii
+4  A: 

This should work fine - can you print your $filename right before pathinfo()?

Edit after you posted your code: so let me get this straight

$imageFile = $_FILES['image']['tmp_name'];
$filenameext = pathinfo($filename, PATHINFO_EXTENSION);

You read in $imageFile but parse an uninitialized variable $filename?

Artem Russakovskii
That's what I thought. It's a submit form but I can print $filename with the result, however it has no extension. I published the full code above if that helps.
You are accessing the wrong variable name, you need to access $imageFile, not $filename.
Artem Russakovskii
Ah, so what would I do to initialize? (newbie)
I saw this too but it seemed too simple..
Ian Elliott
OK, if your name is Dan, but I call you by Bob, you wouldn't know to say "Hey, that's me". Similarly, when you say pathinfo($filename), $filename doesn't exist. You need to call pathinfo($imageFile). Just pay more attention to the little things, read up on the programming basics, and you should be fine.
Artem Russakovskii
You were right! THis did the ticket.$filename = basename( $_FILES['image']['name']);Many thanks!
Heh, if you're renaming the original variable ($imageFile) instead, just don't forget that you need to update any references to it further down in your code.
Artem Russakovskii
A: 

Do not trust the filename extension to describe the file format accurately. Don't trust the mime type either.

$sourceFile = $_FILES['photoupload']['tmp_name'];

list($width, $height, $type, $attr) = getimagesize($sourceFile);

$filetype = image_type_to_extension($type, true);
// $filetype includes the dot.
if ('.jpeg' == $filetype) {
    $filetype = '.jpg';  // use jpg, not the 'jpeg' the function would return
}
Alister Bulman