New thread... new problem (php)

Soldato
Joined
24 Nov 2002
Posts
16,378
Location
38.744281°N 104.846806°W
1) a) How do I force my upload script (VERY basic one I have edited) to rename uploads to lowercase (including the file extension).
b) How do I make it rename .jpeg to .jpg?
c) Would be possible, if I had a textbox, to allow the image to be uploaded renamed with the contents of the textbox?
d) It used to give the user a rejection message if the file type wasn't supported, now it just gives a zero sized reply.... any ideas?

Code:
<?php

$my_max_file_size     = "2097152";     # in bytes
$image_max_width    = "3000";     # in pixels
$image_max_height    = "3000";    # in pixels
$the_path        = "/usr/";

$registered_types = array(
    "image/pjpeg"                => ".jpg, .jpeg",
    "image/jpeg"                => ".jpg, .jpeg",

);

$allowed_types = array("image/pjpeg","image/jpeg");

function form($error=false) {

global $PHP_SELF,$my_max_file_size;

    if ($error) print $error . "<br>";

    print "\n<font size=\"2\" face=\"Tahoma\"><b>Upload a file:</b></font><br><br>";
    print "\n<form ENCTYPE=\"multipart/form-data\" ONSUBMIT=\"alert('Picture will be uploaded!');\" action=\"" . $PHP_SELF . "\" method=\"post\">";
    print "\n<INPUT TYPE=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"" . $my_max_file_size . "\">";
    print "\n<INPUT TYPE=\"hidden\" name=\"task\" value=\"upload\">";
    print "\n<font size=\"2\" face=\"Tahoma\">NOTE: Max file size is " . ($my_max_file_size / 1048576) . "MB, supported file types: .jpg .jpeg .gif";
    print "\n</font><br><INPUT NAME=\"the_file\" TYPE=\"file\" SIZE=\"35\"><br>";
    print "\n<input type=\"submit\" Value=\"Upload\">";
    print "\n</form>";


} # END form

if (!ereg("^4",phpversion())) {
    function in_array($needle,$haystack) { # we have this function in PHP4, so for you PHP3 people
        for ($i=0; $i < count($haystack); $i++) {
            if ($haystack[$i] == $needle) {
                return true;
            }
        }
    }
}


function validate_upload($the_file) {

global $my_max_file_size, $image_max_width, $image_max_height,$allowed_types,$the_file_type,$registered_types;

$start_error = "\n<br><font size=\"2\" face=\"Tahoma\"><b>Error:</b></font>\n<ul>";

    if ($the_file == "none") { # do we even have a file?

        $error .= "\n<li>You did not upload anything!</li>";

    } else { # check if we are allowed to upload this file_type

        if (!in_array($the_file_type,$allowed_types)) {
            $error .= "\n<font size=\"2\" face=\"Tahoma\">The file that you uploaded was of a ".
                "type that is not allowed, you are only
                allowed to upload files of the type:\n<ul>";
            while ($type = current($allowed_types)) {
                $error .= "\n<li>" . $registered_types[$type] . " (" . $type . ")</li>";
                next($allowed_types);
            }
            $error .= "\n</ul></font>";
        }

        if (ereg("image",$the_file_type) && (in_array($the_file_type,$allowed_types))) {

            $size = GetImageSize($the_file);
            list($foo,$width,$bar,$height) = explode("\"",$size[3]);

            if ($width > $image_max_width) {
                $error .= "\n<li>Your image should be no wider than " .
                    $image_max_width . " Pixels</li>";
            }

            if ($height > $image_max_height) {
                $error .= "\n<li>Your image should be no higher than " .
                    $image_max_height . " Pixels</li>";
            }

        }

        if ($error) {
            $error = $start_error . $error . "\n</ul>";
            return $error;
        } else {
            return false;
        }
    }
 } # END validate_upload


function upload($the_file) {

global $the_path,$the_file_name;

    $error = validate_upload($the_file);
    if ($error) {
        form($error);
    } else { # cool, we can continue
        if (!@copy($the_file, $the_path . "/" . $the_file_name)) {
            form("\n<b>FATAL ERROR</b>");
        } else {
    print "\n<font size=\"2\" face=\"Tahoma\"><b>Done :-)</b></font><br><br>";
    print "\n<SCRIPT LANGUAGE=\"JavaScript\">window.location=\"index.php\";</script>";
        }
    }
} # END upload

?>

<?php
switch($task) {
    case 'upload':
        upload($the_file);
    break;
    default:
        form();
}

?>

2) I am using the below to fetch the files in a directory and display them. This works fine, HOWEVER, how do I get it to display ONLY those files that end with ".thumb.jpg" ?

Code:
<?
$dir_handle = @opendir('../pics');
	if ( !$sort ){
	while ($file = readdir($dir_handle)) {
	if ($file != '.htaccess' && $file != '.' && $file != '..' && $file != 'index.php' && $file != 'template.php' && $file != 'white.gif'){
$filenothumb = substr($file, 0, -10);
$file1= basename($filenothumb, ".jpg"); 
    echo "<option value=\"$file\">$file1</option>";
	}
}
}
closedir($dir_handle);

Many thanks guys
 
Caporegime
Joined
18 Oct 2002
Posts
29,490
Location
Back in East London
Take a look at using the function move_uploaded_file in the php manual. Below link is to the upload files page from the manual.

http://www.php.net/manual/en/features.file-upload.php

To ensure the name is lower case, that's simple, when you call move_uploaded_file, use strtolower() for the destination argument:
Code:
$dest = '/path/to/upload/directory/' . strtolower(basename($_FILES['user']['name']));

if (move_uploaded_file($_FILES['user']['tmp_name'], $dest)) {
    echo 'File moved successfully!';
} else {
    echo 'File move unsuccessfull!';
}

To change the exntension from jpeg to jpg, use stri_replace or preg_replace:
Code:
$dest = '/path/to/upload/directory/' . strtolower(basename($_FILES['user']));
$dest = stri_replace('jpeg', 'jpg', $dest); //this will replace all instances of jpeg with jpg
$dest = preg_replace('/\.jpeg$/i', '.jpg', $dest); //my preference, will only replace jpeg with jpg if jpeg is at the end of the string (i.e. the extension of the filename)
 
Last edited:
Soldato
OP
Joined
24 Nov 2002
Posts
16,378
Location
38.744281°N 104.846806°W
I am clumisly using this at the moment:

Code:
<?
$dir_handle = @opendir('../pics');
	if ( !$sort ){
	while ($file = readdir($dir_handle)) {
	if ($file != '.htaccess' && $file != '.' && $file != '..' && $file != 'index.php' && $file != 'template.php' && $file != 'white.gif'){
$filenothumb = substr($file, 0, -10);
$file1= basename($filenothumb, ".jpg");
    echo "<option value=\"$filenothumb\">$file1</option>";
	}
}
}
closedir($dir_handle);
echo "</select>";

Any idea to link/modify that?

The exclusions were there to hide the php files, however, coulnd't I just make it display JUST .thumb.jpg files and skip most of that out?7

Thanks for your continuing help!

EDIT - sorted
 
Last edited:
Caporegime
Joined
18 Oct 2002
Posts
29,490
Location
Back in East London
Code:
echo '<select name="select1">' . "\n";

foreach (glob('/path/to/images/*.thumb.jpg') as $image) {
  echo "\t" . '<option value="' . str_replace('.thumb', '', $image) . '">' . basename($image, '.thumb.jpg') . '</option>' ."\n";
}
echo '</select>';
Should work, but untested. :)
 
Back
Top Bottom