Jump to content

Need File Upload Script Help with Current Script.


phpBeginner06

Recommended Posts

I have not yet leaned about file uploads yet; to a server/web host or to a database. So I found the script and modified it a little bit the PHP and HTML a little bit to function and display as I needed it to. The problem is that I have only one file upload field to upload files with. I tried to add more file fields to HTML, but it would not work because PHP script needs to be modified to upload more than one file at a time. This is where I hope some of you might help me out; if you could - please. I want to be able to upload 9 files at a time. I have the PHP script set to only allow "jpg" and "JPG" files to be uploaded. I want to be able to upload all 9 of my jpg/JPG files at one time.

Does any one know how I can accomplish this with the PHP and HTML code I have below?

[u]PHP Code[/u]  [color=red][size=9pt]Notice: PHP Code file is named "uploadFile.php"[/size][/color]

[code]<?php

// Begin options

$allow_file_deletion = true; // To allow visitors to delete files, leave this at true; otherwise, change it to false

$file_extensions = array(".jpg", ".JPG"); // Add or delete the file extensions you want to allow

$file_extensions_list = ".jpg, .JPG"; // Type the same as above, without the quotes separating them

$max_length = 500; // The maximum character length for a file name

$maximum_file_size = "1000000000"; // In bytes

$upload_log_file = ""; // Change this to the log file you want to use

// End options

$folder_directory = "http://".$_SERVER["HTTP_HOST"].dirname($_SERVER["PHP_SELF"]);
$message = "";
$set_chmod = 0;
$site_uri = "http://mydomain.com/webpage.php";
$upload_directory = "subdirectorynamehere/";
$upload_uri = $folder_directory."/subdirectorynamehere/";

if($allow_file_deletion == true) $status = "enabled";
else $status = "disabled";

if($_REQUEST["delete"] && $allow_file_deletion) {
$resource = fopen($upload_log_file,"a");
fwrite($resource,date("F d, Y / h:i:sa")." - ".$_REQUEST["delete"]." deleted by ".$_SERVER["REMOTE_ADDR"]."\n");
fclose($resource);

if(strpos($_REQUEST["delete"],"/.") > 0);
elseif(strpos($_REQUEST["delete"],$upload_directory) === false);
elseif(substr($_REQUEST["delete"],0,6) == $upload_directory) {
unlink($_REQUEST["delete"]);
$message = "File has been deleted.";
header("Location: $site_uri?message=$message");
}
}

elseif($_FILES["userfile"]) {
$resource = fopen($upload_log_file,"a");
fwrite($resource,date("F d, Y / h:i:sa")." - ".$_FILES["userfile"]["name"]." "
.$_FILES["userfile"]["type"]." uploaded by ".$_SERVER["REMOTE_ADDR"]."\n");
fclose($resource);

$file_type = $_FILES["userfile"]["type"];
$file_name = $_FILES["userfile"]["name"];
$file_ext = strtolower(substr($file_name,strrpos($file_name,".")));
chmod($upload_uri."".$file_name, 0755);
if($_FILES["userfile"]["size"] > $maximum_file_size) {
$message = "ERROR: File size cannot be over ".$maximum_file_size." bytes.<br><br>";
}

elseif($file_name == "") $message = "ERROR: Please select a file to upload.<br><br>";
elseif(strlen($file_name > $max_length)) $message = "ERROR: The maximum length for a file name is ".$max_length." characters.<br><br>";
elseif(!preg_match("/^[A-Z0-9_.\- ]+$/i",$file_name)) $message = "ERROR: Your file name contains invalid characters.<br><br>";
elseif(!in_array($file_ext, $file_extensions))
$message = "ERROR: <ins>$file_ext</ins> is not an allowed file extension.<br><br>";
else $message = upload_file($upload_directory, $upload_uri);
header("Location: $site_uri?message=$message");
}

elseif(!$_FILES["userfile"]);
else $message = "ERROR: Invalid file specified.<br><br>";

$open = opendir($upload_directory);
$uploaded_files = "";
while($file = readdir($open)) {
if(!is_dir($file) && !is_link($file)) {
$uploaded_files .= " <tr>
<td style=\"background: #fff; color: #000; text-align: left; width: 70%\"><a href=\"$upload_directory$file\" title=\"$file (".filesize($upload_directory."".$file)." bytes)\">".$file."</a> (".filesize($upload_directory."".$file)." bytes)</td>";
if($allow_file_deletion)
$uploaded_files .= "
<td style=\"background: #fff; color: #000; text-align: right; width: 30%\"><a href=\"?delete=$upload_directory".urlencode($file)."\" title=\"Delete File\">Delete File</a></td>";
else
$uploaded_files .= "
<td style=\"background: #fff; color: #000; text-align: right; width: 30%\"><del><strong>Delete File</strong></del></td>";
$uploaded_files .= "
</tr>
<tr>
<td colspan=\"2\" style=\"background: #eee; color: #000; text-align: left; text-indent: 20px\">Uploaded <strong>".date("F d, Y / h:ia", filemtime($upload_directory.$file))."</strong></td>";
$uploaded_files .="
</tr>
";
}
}

function upload_file($upload_directory, $upload_uri) {
$file_name = $_FILES["userfile"]["name"];
$file_name = str_replace(" ","_",$file_name);
$file_path = $upload_directory.$file_name;
$temporary = $_FILES["userfile"]["tmp_name"];

$result = move_uploaded_file($temporary, $file_path);
if(!chmod($file_path,0777))
$message = "ERROR: A folder to place the files was not found.";
else $message = ($result)?"File has been uploaded.<br><br>" : "An error has occurred.<br><br>";
return $message;
}
?>[/code]

[u]HTML Code[/u]

[code]<form action="http://mydomain.com/subdirectoryname/uploadFile.php" enctype="multipart/form-data" id="upload" method="post">
<span style="color:red;font-family:arial bold;font-size:100%;font-weight:bold"></a>
<input id="userfile" name="userfile" size="45" type="file" /> <input name="upload" type="submit" value="Upload File" />
</form>[/code]
Well first of all you need to make a for loop which loops through the script 9 times. Second you need to put the 9 form fields into an array:

[code]
<?php
for($i = 1; $i > 9; $++){
?>
<form method="post" enctype="multidata/form-data">
<input="file" name="image[]">
</form>
<?php
}
?>
[/code]

here you get 9 forms which are stored in a array.

then you need to foreach the array in order to use the nine forms seperatly and store the data in the database.

Anohter thing, i dont see anything in the code that indicates that you want to update the database?
I prefer the while loop method. That way it's irrelevant if the user only uploads one file, or 12 files.

you just need to replace 'userfile' with key($_FILES), which would trivialize the actual field name.

[code]<?php
if($_FILES) do {
    ... $_FILES[key($_FILES)]['tmp_name'] ...
    ... $_FILES[key($_FILES)]['name'] ...
    ... $_FILES[key($_FILES)]['type'] ...
}while(next($_FILES));
?>[/code]

in your upload form, you'll need to change your upload filenames, too. name="image[]" that JJohnsenDK recommended should be okay, or you can just call them 'userfile1', 'userfile2', etc.
So I tried the example that "michaellunsford" suggested and I probably did not do it right, but now it does not upload at all. Here is the code below:

[quote]<?php

// Begin options

$allow_file_deletion = true; // To allow visitors to delete files, leave this at true; otherwise, change it to false

$file_extensions = array(".jpg", ".JPG"); // Add or delete the file extensions you want to allow

$file_extensions_list = ".jpg, .JPG"; // Type the same as above, without the quotes separating them

$max_length = 500; // The maximum character length for a file name

$maximum_file_size = "1000000000"; // In bytes

$upload_log_file = ""; // Change this to the log file you want to use

// End options

$folder_directory = "http://".$_SERVER["HTTP_HOST"].dirname($_SERVER["PHP_SELF"]);
$message = "";
$set_chmod = 0;
$site_uri = "http://mydomain.com/webpage.php";
$upload_directory = "subdirectorynamehere/";
$upload_uri = $folder_directory."/subdirectorynamehere/";

if($allow_file_deletion == true) $status = "enabled";
else $status = "disabled";

if($_REQUEST["delete"] && $allow_file_deletion) {
$resource = fopen($upload_log_file,"a");
fwrite($resource,date("F d, Y / h:i:sa")." - ".$_REQUEST["delete"]." deleted by ".$_SERVER["REMOTE_ADDR"]."\n");
fclose($resource);

if(strpos($_REQUEST["delete"],"/.") > 0);
elseif(strpos($_REQUEST["delete"],$upload_directory) === false);
elseif(substr($_REQUEST["delete"],0,6) == $upload_directory) {
unlink($_REQUEST["delete"]);
$message = "File has been deleted.";
header("Location: $site_uri?message=$message");
}
}

elseif($_FILES["key($_FILES)"]) {
$resource = fopen($upload_log_file,"a");
fwrite($resource,date("F d, Y / h:i:sa")." - ".$_FILES["key($_FILES)"]["name"]." "
.$_FILES["key($_FILES)"]["type"]." uploaded by ".$_SERVER["REMOTE_ADDR"]."\n");
fclose($resource);

$file_type = $_FILES["key($_FILES)"]["type"];
$file_name = $_FILES["key($_FILES)"]["name"];
$file_ext = strtolower(substr($file_name,strrpos($file_name,".")));
chmod($upload_uri."".$file_name, 0755);
if($_FILES["key($_FILES)"]["size"] > $maximum_file_size) {
$message = "ERROR: File size cannot be over ".$maximum_file_size." bytes.<br><br>";
}

elseif($file_name == "") $message = "ERROR: Please select a file to upload.<br><br>";
elseif(strlen($file_name > $max_length)) $message = "ERROR: The maximum length for a file name is ".$max_length." characters.<br><br>";
elseif(!preg_match("/^[A-Z0-9_.\- ]+$/i",$file_name)) $message = "ERROR: Your file name contains invalid characters.<br><br>";
elseif(!in_array($file_ext, $file_extensions))
$message = "ERROR: <ins>$file_ext</ins> is not an allowed file extension.<br><br>";
else $message = upload_file($upload_directory, $upload_uri);
header("Location: $site_uri?message=$message");
}

elseif(!$_FILES["key($_FILES)"]);
else $message = "ERROR: Invalid file specified.<br><br>";

$open = opendir($upload_directory);
$uploaded_files = "";
while($file = readdir($open)) {
if(!is_dir($file) && !is_link($file)) {
$uploaded_files .= " <tr>
<td style=\"background: #fff; color: #000; text-align: left; width: 70%\"><a href=\"$upload_directory$file\" title=\"$file (".filesize($upload_directory."".$file)." bytes)\">".$file."</a> (".filesize($upload_directory."".$file)." bytes)</td>";
if($allow_file_deletion)
$uploaded_files .= "
<td style=\"background: #fff; color: #000; text-align: right; width: 30%\"><a href=\"?delete=$upload_directory".urlencode($file)."\" title=\"Delete File\">Delete File</a></td>";
else
$uploaded_files .= "
<td style=\"background: #fff; color: #000; text-align: right; width: 30%\"><del><strong>Delete File</strong></del></td>";
$uploaded_files .= "
</tr>
<tr>
<td colspan=\"2\" style=\"background: #eee; color: #000; text-align: left; text-indent: 20px\">Uploaded <strong>".date("F d, Y / h:ia", filemtime($upload_directory.$file))."</strong></td>";
$uploaded_files .="
</tr>
";
}
}

function upload_file($upload_directory, $upload_uri) {
if($_FILES) do {
$file_name = $_FILES["key($_FILES)"]["name"];
$file_name = str_replace(" ","_",$file_name);
$file_path = $upload_directory.$file_name;
$temporary = $_FILES["key($_FILES)"]["tmp_name"];

$result = move_uploaded_file($temporary, $file_path);
if(!chmod($file_path,0777))
$message = "ERROR: A folder to place the files was not found.<br><br>";
else $message = ($result)?"File has been uploaded.<br><br>" : "An error has occurred.<br><br>";
return $message;
}while(next($_FILES));
?>[/quote]

So what did I do wrong; any one know?
first thing that jumps out
[code]elseif($_FILES["key($_FILES)"])[/code]
key is a function, not a variable name, therefore it should not appear in quotes. you've got this scattered across your code.

should be
[code]elseif($_FILES[key($_FILES)])[/code]

in the particular instance of the elseif statement, what you have should work (minus the quotes) but just checking for $_FILES would work, too.
[code]elseif($_FILES)[/code]
Thanks For Your Help "michaellunsford" and "JJohnsenDK", but since I last posted this script; I found another script, on another website. The script I just found on this other website has a multi-upload option, but does not have "allowed file type" security in the script. That's ok with me, I already know who is uploading these files with this file upload script (me!!!). So I know what files I will allow myself to upload - LOL!!!

Just in case someone else is looking for a script like this; visit this webpage below. It worked out really good for me and maybe it will for you too.

[url=http://www.plus2net.com/php_tutorial/php_multi_file_upload.php]http://www.plus2net.com/php_tutorial/php_multi_file_upload.php[/url]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.