I have changed a script, now it's working very nice.
I have searched into google, and on differend forum's but I don't understand or how to solve my problem in this script.
My purphose is to rename the uploaded filename to my variable: $flnm the extension I do not now and have to stay the same.
example: city.gif have to change to 56.gif if $flnm=56.
The hole thing is to grab the file-name, and change the part before the dot
Are there function the grab the first part of the file-name, so yes, do you have an example?
You can find the script
The only thing that you have to do is changing the path to you uploading directory.
Tips are welcome
thanks in advance.
<?php
$my_max_file_size = "40960"; # in bytes
$image_max_width = "600";
$image_max_height = "600";
//$the_path = "/usr/local/apache/htdocs/sites/dev/phpbuilder/upload/files";
$the_path = "/usr/local/psa/home/vhosts/domain.area/httpdocs/image/"; //the file path where to store
$flnm = "new name"; //the name of the new file, without extension
$registered_types = array(
"application/x-gzip-compressed" => ".tar.gz, .tgz",
"application/x-zip-compressed" => ".zip",
"application/x-tar" => ".tar",
"text/plain" => ".html, .php, .txt, .inc (etc)",
"image/bmp" => ".bmp, .ico",
"image/gif" => ".gif",
"image/pjpeg" => ".jpg, .jpeg",
"image/jpeg" => ".jpg, .jpeg",
"application/x-shockwave-flash" => ".swf",
"application/msword" => ".doc",
"application/vnd.ms-excel" => ".xls",
"application/octet-stream" => ".exe, .fla (etc)"
); # these are only a few examples, you can find many more!
$allowed_types = array("image/bmp","image/gif","image/pjpeg","image/jpeg");
--
function form($error=false) {
global $PHP_SELF,$my_max_file_size;
if ($error) print $error . "<br><br>";
echo "\n<form ENCTYPE=\"multipart/form-data\" action=\"" . $PHP_SELF . "?flnm=$flnm\" method=\"post\">";
echo "\n<INPUT TYPE=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"" . $my_max_file_size . "\">";
echo "\n<INPUT TYPE=\"hidden\" name=\"task\" value=\"upload\">";
echo "\n<P>Upload a file";
echo "\n<BR>NOTE: Max file size is " . ($my_max_file_size / 1024) . "KB";
echo "\n<br><INPUT NAME=\"the_file\" TYPE=\"file\" SIZE=\"40\"><br>";
echo "\n<input type=\"submit\" Value=\"Upload\">";
echo "\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<b>Error:</b>\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<li>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>";
}
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 list_files() {
global $the_path;
$handle = dir($the_path);
print "\n<b>Uploaded files:</b><br>";
while ($file = $handle->read()) {
if (($file != ".") && ($file != "..")) {
print "\n" . $file . "<br>";
}
}
print "<hr>";
}
--
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>Something barfed, check the path to and the permissions for the upload directory</b>");
} else {
list_files();
form();
}
}
} # END upload
--
//if (ereg("jpeg" or "pjpeg" or "jpg",$the_file_name)) ($extens .=".jpg"); //zien of file jpg is
//if (ereg("bmp",$the_file_name)) ($extens .=".bmp"); //zien of file jpg is
//if (ereg("gif",$the_file_name)) ($extens .=".gif"); //zien of file jpg is
//$gva = $flnm;//filetype ($the_file_name);
//$gva = $gva . $extens;
############ Start page
print "<html>\n<head>\n<title>Adding a picture</title>\n</head>\n<body>";
switch($task) {
case 'upload':
upload($the_file);
break;
default:
form();
}
echo $extens . "\n ex<br>"; //test to view the extension name.
echo "\n</body>\n</html>";
?>