Page 1 of 1
File Uploading Script
Posted: Fri Mar 07, 2003 9:34 am
by nigma
Hey I have tried multiple tutorials on how to make a file upload script and none have worked. I get an error each time. IS there any configuring I need to do before it will work? I mean like change some settings in the php.ini file?
Thanks a bunch for all help and advice provided.
Posted: Fri Mar 07, 2003 10:38 am
by daven
viewtopic.php?t=6435
Check out that thread. Rather long discussion and lots of examples for file uploading scripts.
Posted: Fri Mar 07, 2003 10:40 am
by samsonite451
I had similar problems the first time I tried to write an upload script. The problem was that most of the tutorials I found relied on variables that are off by default (the register_globals setting in php.ini). Simply setting register_globals to on will make most of them work. However, as you'll find multiple times throughout the documentation, register_globals is set to Off by default for a reason, and it's best to familiarize yourself with the superglobals like $_GET, $_POST, $_FILES, etc.
Check this link for more info:
http://www.php.net/manual/en/language.v ... efined.php
Posted: Fri Mar 07, 2003 10:42 am
by rodrigocaldeira
Se the code:
Code: Select all
<form method=post action="yourfile.php" enctype="multipart/form-data">
<input type="file" name="userfile">
<input type="submit" value="UpLoad">
This is the HTML form
In yourfile.php you can read your file data, as type, size and name.
Use $userfile to manipulate the file in the server. You can copy to another path with copy($userfile,"/path/to/copy/filename");
Use $userfile_name to show the real file name. So, on the copy statement you can do this: copy($userfile,"path/to/copy/$userfile_name");
Use $userfile_type to show the MIME type of uploaded file
Use $userfile_size to show the file size.
Note.: I've used $userfile on this case, but you decide the form field name.
The default on php.ini is upload files with maxsize 2M.
Posted: Mon Mar 10, 2003 3:25 am
by twigletmac
rodrigocaldeira - this only works if register_globals is on, since it's now off by default this code would not work for a lot of people.
samsonite451 is right, it's a really good idea to get to grips with the superglobals.
Mac
_
Posted: Mon Mar 10, 2003 4:03 am
by deejay
i found this tutorial on uploading images very easy to understand and implement. Would have thought file uploads work in the same way, although I havent tried it yet (newbie)
http://www.phpfreaks.com/tutorials/36/0.php
Posted: Mon Mar 10, 2003 11:53 am
by whiterockhosting
Here's a simple script I wrote that allows me to quickly throw in a file upload anywhere I need it.
http://www.nebjamin.com/upload/upload.phps
<?php
// Number of Files to upload
$MAX_FILES = 8;
//Upload Directory
$UPLOAD_DIR = "/var/www/images/";
$WWW_DIR = "images/";
// Limit file extnsions
$extensions = array(".jpg",".gif",".psd",".ai",".txt",".csv");
//======================================================
// Decide on action to take
//======================================================
switch($action) {
case "Upload":
doSave();
include_once("../includes/header.php");
displayMessage();
displayForm();
displayCurrentFiles();
include_once("includes/footer.php");
break;
default:
include_once("../includes/header.php");
displayBusinessRules();
displayForm();
displayCurrentFiles();
include_once("includes/footer.php");
break;
}
//======================================================
//======================================================
Function doSave() {
GLOBAL $_FILES;
GLOBAL $UPLOAD_DIR;
GLOBAL $extensions;
GLOBAL $message;
foreach( $_FILES AS $currentFile ) {
$ext = strrchr($currentFile['name'],'.');
if ($currentFile['size'] > 0 ) {
if ( !in_array(strtolower($ext),$extensions) ) {
$message .= "<br>". $currentFile['name'] ." was not successfully uploaded because it did not match the file type r
equirements. Please upload files with type: ";
foreach ( $extensions AS $extension ) {
$message .= " $extension";
}
} else {
if ( is_dir( $UPLOAD_DIR ) ) {
copy( $currentFile['tmp_name'], $UPLOAD_DIR.$currentFile['name'])
or die("Fatal Error: Couldn't copy the file!");
$message .= "<br>". $currentFile['name'] ." was uploaded successfully.";
} else {
$message .= "<br><br>Fatal Error: The upload directory was not found. Please contact the system administrator
.";
}
}
} else {
if ( $currentFile['name'] ) {
$message .= "<br>". $currentFile['name'] ." was not successfully uploaded because it was empty.";
}
}
}
}
Function displayMessage() {
GLOBAL $message;
echo "<br>$message<br><br>";
}
Function displayCurrentFiles() {
GLOBAL $UPLOAD_DIR;
GLOBAL $WWW_DIR;
echo "The following files are currently available on the server:";
echo "<br><br>";
if(is_dir($UPLOAD_DIR)) {
if ($dir_handle = opendir($UPLOAD_DIR)) {
//$dir_handle = opendir($UPLOAD_DIR);
while ( false !== ($file = readdir($dir_handle))) {
if ($file != "." && $file != ".." && $file != "index.php") {
echo "<li><a href=\"". $WWW_DIR . $file ."\" target=\"_preview\">$file</a>\n";
}
}
} else {
echo "There are currently no files available on the server.";
}
closedir($dir_handle);
} else {
echo "The selected directory is not available on the server.";
}
}
Function displayForm() {
GLOBAL $PHP_SELF;
GLOBAL $MAX_FILES;
GLOBAL $extensions;
GLOBAL $UPLOAD_DIR;
GLOBAL $WWW_DIR;
echo "<br><br>";
echo "Please select file(s) to upload to the server (";
foreach ($extensions AS $extension) {
echo " $extension";
}
echo " only):";
echo "<form enctype='multipart/form-data' action=\"". $PHP_SELF ."\" method=\"post\" name=\"uploadForm\">";
for ( $filenumber = 1; $filenumber <= $MAX_FILES; $filenumber++ ) {
echo "<br>$filenumber. <input type=\"file\" name=\"upfilename_$filenumber\" size=\"40\">";
}
echo "<br><br><input type=\"submit\" name=\"action\" value=\"Upload\">";
echo "</form>";
}
Function displayBusinessRules() {
}