I've been modify this php script but it won't work, it always fail. It managed to create the folder, but it fails to move the files from the temporary folder to the right one, the function move_uploaded_file return always false. This is the code:
<?php
include 'connection.php';
include '../empty.html';
session_start();
if(isset($_FILES['filearray'])){
$name_array = $_FILES['filearray']['name'];
$tmp_name_array = $_FILES['filearray']['tmp_name'];
$type_array = $_FILES['filearray']['type'];
$size_array = $_FILES['filearray']['size'];
$error_array = $_FILES['filearray']['error'];
$titlealbum=$_POST['titoloalbum'];
$username=$_SESSION['username'];
$path="../users/".$username."/".$titlealbum."/";
echo $path;
mkdir($path,0777);
$total=count($tmp_name_array);
for($i=0; $i<$total; $i++){
$rightpath=$path.$name_array[$i];
if(move_uploaded_file($tmp_name_array[$i], $rightpath)){
echo $name_array[$i]." upload is complete<br>";
echo "upload completato";
} else {
echo "move_uploaded_file function failed for ".$name_array[$i]." into".$path."<br>";
}
}
}
else
echo "Files not found";
?>
This is the html form:
<form id="albumform" style="display:none" enctype="multipart/form-data" action="scripts/albumupload.php" multiple="multiple" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="30000000">
Name: <input name="titoloalbum" type="text" required><br><br>
Cover: <input name="userfile" type="file">
<br><br>Select your songs:<br />
<input name="filearray[]" type="file" value="10000000" /><br />
<input name="filearray[]" type="file" value="10000000"/><br />
<input name="filearray[]" type="file" value="10000000"/><br />
<input name="filearray[]" type="file" value="10000000"/><br />
<input type="submit" value="Send files" />
</form>
I know that this form kinda sucks, but i don't like the multiple selection with a signle "input". Thanks in advice
You have error in your code :
$total=count($tmp_name_array);
change this to
$total=count($name_array);
You are using count function with wrong variable.
Also remove so many file types with same name from the form. Either name them different.
<input name="filearray[]" type="file" value="10000000"/><br />
You could use the following commented algorithm for Multiple-Files upload:
PHP
<?php
// FILENAME: albumupload.php
include 'connection.php';
include '../empty.html';
session_start();
$filesArray = isset( $_FILES['filesArray'] ) ? $_FILES['filesArray'] : null;
$titleAlbum = isset($_POST['titoloalbum']) ? isset($_POST['titoloalbum']) : null;
$arrFilesData = array();
if( $filesArray && !empty($filesArray) ){
$arrFilesKeys = array_keys($filesArray['name']);
$arrFilesNames = $filesArray['name'];
$arrFilesTypes = $filesArray['type'];
$arrFilesTmpNames = $filesArray['tmp_name'];
$arrFilesErrors = $filesArray['error'];
$arrFilesSizes = $filesArray['size'];
foreach($arrFilesKeys as $intKey=>$strKeyName){
$tempFileData = new stdClass();
$tempFileData->key = $strKeyName;
$tempFileData->name = $arrFilesNames[$strKeyName];
$tempFileData->type = $arrFilesTypes[$strKeyName];
$tempFileData->tmp_name = $arrFilesTmpNames[$strKeyName];
$tempFileData->error = $arrFilesErrors[$strKeyName];
$tempFileData->error = $arrFilesSizes[$strKeyName];
$arrFilesData[$strKeyName] = $tempFileData;
}
// UPLOAD THE FILES:
if($titleAlbum){
$username = trim($_SESSION['username']);
$path = __DIR__ . "/../users/" . $username . "/" . $titleAlbum;
//CREATE UPLOAD DIRECTORY IF IT DOESN'T ALREADY EXIST...
if(!file_exists($path)){
mkdir($path, 0777, TRUE);
}
// LOOP THROUGH THE FILES OBJECT ARRAY AND PERFORM FILE-UPLOAD
foreach($arrFilesData as $fileKey=>$objFileData){
$rightPath = $path . DIRECTORY_SEPARATOR . $objFileData->name;
if(move_uploaded_file($objFileData->tmp_name, $rightPath)){
echo $objFileData->name . " upload is complete<br>";
echo "upload completato";
} else {
echo "move_uploaded_file function failed for ". $objFileData->name ." into". $path . "<br>";
}
}
}
}
In this case, your HTML Form is expected to look like this:
HTML
<form id="albumform" style="" enctype="multipart/form-data" action="scripts/albumupload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="30000000">
Name: <input name = "titoloalbum" type="text" required><br><br>
Cover: <input name = "filesArray[userfile]" type="file">
<br><br>Select your songs:<br />
<input name="filesArray[file_1]" type="file" value="" /><br />
<input name="filesArray[file_2]" type="file" value=""/><br />
<input name="filesArray[file_3]" type="file" value=""/><br />
<input name="filesArray[file_4]" type="file" value=""/><br />
<input type="submit" value="Send files" />
</form>
Related
Actually I could duplicate my upload.php, change the file names and call it a day. But how can you do what I'm trying to do with only one upload.php?
My upload script changes the file name to msg1.txt.... I have two forms, how can I call the same upload.php with each for and get mes1.txt, and if form two mes2.txt?
this is all under a local domain so security is not an issue...
here is my form...
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
any help in this area is appreciated. Thank you..
here is my upload.php
<?php
$destination_path = $_REQUEST["destination"] . "upload/";
$target_path = "" . $destination_path;
$target_path = $target_path . 'msg1.txt';
$_POST['dest_name'];
echo "User=" . $_ENV[USER] . "<br />";
echo "Source=" . $_FILES['uploadedfile']['name'] . "<br />";
echo "Destination=" . $destination_path . "<br />";
echo "Target path=" . $target_path . "<br />";
echo "Size=" . $_FILES['uploadedfile']['size'] . "<br />";
//echo "Tmp name=" . $_FILES['uploadedfile']['tmp_name'] . "<br />";
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>
I would add a hidden input in both forms, called for example msg_type but with different values in each one,
Your first form
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<input type="hidden" name="msg_type" value="1" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
Your second form
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<input type="hidden" name="msg_type" value="2" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
Then in your script
$message_type = $_POST['msg_type'];
if ($message_type == 1) {
$target_path = $target_path . 'msg1.txt';
}
else if ($message_type == 2) {
$target_path = $target_path . 'msg2.txt';
}
else {
$target_path = $target_path . 'unknown_form.txt';
}
You get from $_POST the value of the hidden field you had set on the form and then you proceed as you need to
Ok I am new to mysql and php array system. I need this to work with my simple file script I have made. Below is the code like I said it works find. But I can not figure out how to add the extra inputs...
if(isset($_FILES["file"])) {
if ($_FILES["file"]["error"] > 0) {
// Error
$up = 0;
} else {
$name = $_FILES["file"]["name"];
move_uploaded_file($_FILES["file"]["tmp_name"], UPLOADS."/$name");
$up = 1;
}
header('Location: upload.php?upload='.$up);
}
if(!$steam->isLoggedin()) {
echo "Sorry Guest, You need to login with steam to upload! <br /> \n \n";
} else {
// Check our return status to see if it worked or not
if(isset($_GET['upload'])) {
if($_GET['upload'] == 1) {
echo 'Uploaded succesfully';
} else {
echo 'Upload failed';
}
}
}
<form action="upload.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<h1>Upload A ScreenShot</h1>
<em>Photo Name:</em><input name="name" type="text" id="name" />
<br />
<em>Size:</em><input name="size" type="text" id="size" />
<br />
<em>Category</em>
<select id="category" name="category">
<option>ScreenShots</option>
</select>
<br />
<em>Upload</em>:<input type="file" name="file" />
<br />
<input type="submit" name="Submit" value="Submit" />
</form>
The upload part works. But what I can not do is figure out how to add Size, Description and Category to the php code. I just do not know how to
set that part up and I do not know were to place the code. If anyone could help me I would really apricate it :).
$name = isset($_POST['name']); just returns true what you want to do with this line:
$filename = $name."_"."$type"."_".$rand."_".$_FILES['file']['name'];
and don't forget about enctype= multipart/form-data in html whithout it you could not save your file
I am trying to upload multiple images to a folder using PHP using this tutorial I managed:
In the PHP form
<?php
$success = 0;
$fail = 0;
$uploaddir = 'uploads/';
for ($i=0;$i<4;$i++)
{
if($_FILES['userfile']['name'][$i])
{
$uploadfile = $uploaddir . basename($_FILES['userfile']['name'][$i]);
$ext = strtolower(substr($uploadfile,strlen($uploadfile)-3,3));
if (preg_match("/(jpg|gif|png|bmp)/",$ext))
{
if (move_uploaded_file($_FILES['userfile']['tmp_name'][$i], $uploadfile))
{
$success++;
}
else
{
echo "Error Uploading the file. Retry after sometime.\n";
$fail++;
}
}
else
{
$fail++;
}
}
}
echo "<br> Number of files Uploaded:".$success;
echo "<br> Number of files Failed:".$fail;
?>
In the HTML form
<form enctype="multipart/form-data" action="upload.php" method="post">
Image1: <input name="userfile[]" type="file" /><br />
Image2: <input name="userfile[]" type="file" /><br />
Image3: <input name="userfile[]" type="file" /><br />
Image4: <input name="userfile[]" type="file" /><br />
<input type="submit" value="Upload" />
</form>
As you can see in the HTML form the input name is userfile[] for all of them. Now in my HTML for the input names are as follows: picture01, picture02, picture 03, etc...
How can I modify the PHP code to have my input names {: picture01, picture02, picture 03} rather than userfile[].
Thanks.
UPDATE
I want the above to fit in my HTML Form
<form enctype="multipart/form-data" action="upload.php" method="post">
Picture 01<input id="picture01" name="picture01" type="file" ><br />
Picture 02<input id="picture02" name="picture02" type="file" ><br />
Picture 03<input id="picture03" name="picture03" type="file" ><br />
Picture 04<input id="picture04" name="picture04" type="file" ><br />
<input type="submit" value="Upload" />
</form>
This code is working locally. It uses a combination of your code and the example from php.net. You should probably use pathinfo to get the extension but that's a minor detail.
form.html
<form enctype="multipart/form-data" action="upload.php" method="post">
Image1: <input name="userfile[]" type="file" /><br />
Image2: <input name="userfile[]" type="file" /><br />
Image3: <input name="userfile[]" type="file" /><br />
Image4: <input name="userfile[]" type="file" /><br />
<input type="submit" value="Upload" />
</form>
upload.php:
<?php
error_reporting(E_ALL);
ini_set("display_errors",1);
$success = 0;
$fail = 0;
$uploads_dir = 'uploads';
$count = 1;
foreach ($_FILES["userfile"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["userfile"]["tmp_name"][$key];
$name = $_FILES["userfile"]["name"][$key];
$uploadfile = "$uploads_dir/$name";
$ext = strtolower(substr($uploadfile,strlen($uploadfile)-3,3));
if (preg_match("/(jpg|gif|png|bmp)/",$ext)){
$newfile = "$uploads_dir/picture".str_pad($count++,2,'0',STR_PAD_LEFT).".".$ext;
if(move_uploaded_file($tmp_name, $newfile)){
$success++;
}else{
echo "Couldn't move file: Error Uploading the file. Retry after sometime.\n";
$fail++;
}
}else{
echo "Invalid Extension.\n";
$fail++;
}
}
}
echo "<br> Number of files Uploaded:".$success;
echo "<br> Number of files Failed:".$fail;
When you have change the names in the form, change the name when you try to get an array element of the file
ex.
echo $_FILES["picture$i"]['name'];
and change the for as this
for ($i=1;$i<=4;$i++)
i have a form which is able to upload multiple selected files to the folder. I just need to find how how to insert them into the database using PHP?
HTML form
<form name="demoFiler" id="demoFiler" enctype="multipart/form-data">
<input type="file" name="multiUpload" id="multiUpload" multiple />
<input type="submit" name="submitHandler" id="submitHandler" value="Upload" class="buttonUpload" />
</form>
PHP code
if($_SERVER['REQUEST_METHOD'] == "POST"){
if(move_uploaded_file($_FILES['file']['tmp_name'], "uploads/".$_FILES['file']['name'])){
echo($_POST['index']);
}
exit;
}
This is my code and first of all I can't get the files to be moved to the upload folder:
<?php
$username = "root";
$password = "";
$host = "localhost";
$database = "db_holiday";
// Make the connect to MySQL or die
// and display an error.
$link = mysql_connect($host, $username, $password);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
// Select your database
mysql_select_db ($database);
if($_SERVER['REQUEST_METHOD']=="POST")
{
foreach ($_FILES['multiUpload']['name'] as $fileName) {
$uploaddir= '/upload/';
$uploadfile = $uploaddir . basename($fileName);
if (move_uploaded_file($fileName, $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}
}
}
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="multiUpload[]" id="multiUpload" multiple="multiple" />
<input type="submit" name="button"/>
</form>
HTML:
<input type="file" name="multiUpload[]" id="multiUpload" multiple />
php:
foreach ($_FILES['multiUpload'] as $fileName) {
//write your code here
//$fileName
}
Use 2 input type="file" elements
<input type="file" name="img_1" />
<input type="file" name="img_2" />
Access each file using
$_FILES['img_1'][];
$_FILES['img_2'][];
Or you can also use an array like
<input type="file" name="img[]" />
<input type="file" name="img[]" />
<!--Which is often used in checkboxes-->
And access each image using
$_FILES['img']['tmp_name'][0];
$_FILES['img']['tmp_name'][1];
Trying to build a admin panel for uploading text and images here is the html page. The Html page is working fine it's the php page that is broken the html is just here for reference.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<title>Administartor Panel</title>
<head>
<link rel="stylesheet" type="text/css" href="admin.css" />
</head>
<body>
<h1>Heritage House Administartor Panel</h1>
<br/>
<h2>
Event One
Event Two
Event Three
Event Four
Event Five
Event Six
</h2>
<br/>
<table>
<tr>
<td id="eone">
<br/>
<p>Event One</P>
<p> Please name the picture file1 before uploading.</p>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="afile" id="afile" />
<br />
<input type="submit" name="submit" value="submit" />
</form>
<form action="WriteTxt1.php" method="post"
enctype="multipart/form-data">
<label for="file">Input Text for artical One:</label>
<textarea rows="25" cols="100" name="content">
</textarea>
<br />
<input type="submit" name="submit" value="Save" />
</form>
</td>
</tr>
<tr>
<td id="etwo">
<p >Event Two</P>
<p> Please name the picture file2 before uploading.</p>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="bfile" id="bfile" />
<br />
<input type="submit" name="submit" value="submit" />
</form>
<form action="WriteTxt2.php" method="post"
enctype="multipart/form-data">
<label for="file">Input Text for artical Two:</label>
<textarea rows="25" cols="100" name="content">
</textarea>
<br />
<input type="submit" name="submit" value="Save" />
</form>
</td>
</tr>
<tr>
<td id="ethree" >
<p >Event Three</P>
<p> Please name the picture file3 before uploading.</p>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="cfile" id="cfile" />
<br />
<input type="submit" name="submit" value="submit" />
</form>
<form action="WriteTxt3.php" method="post"
enctype="multipart/form-data">
<label for="file">Input Text for artical Three:</label>
<textarea rows="25" cols="100" name="content">
</textarea>
<br />
<input type="submit" name="submit" value="Save" />
</form>
</td>
</tr>
<tr>
<td id="efour" >
<p >Event Four</P>
<p> Please name the picture file4 before uploading.</p>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="dfile" id="dfile" />
<br />
<input type="submit" name="submit" value="submit" />
</form>
<form action="WriteTxt4.php" method="post"
enctype="multipart/form-data">
<label for="file">Input Text for artical Four:</label>
<textarea rows="25" cols="100" name="content">
</textarea>
<br />
<input type="submit" name="submit" value="Save" />
</form>
</td>
</tr>
<tr>
<td id="efive" >
<p >Event Five</P>
<p> Please name the picture file5 before uploading.</p>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="efile" id="efile" />
<br />
<input type="submit" name="submit" value="submit" />
</form>
<form action="WriteTxt5.php" method="post"
enctype="multipart/form-data">
<label for="file">Input Text for artical Five:</label>
<textarea rows="25" cols="100" name="content">
</textarea>
<br />
<input type="submit" name="submit" value="Save" />
</form>
</td>
</tr>
<tr>
<td id="esix" >
<p >Event Six</P>
<p> Please name the picture file6 before uploading.</p>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="ffile" id="file6" />
<br />
<input type="submit" name="submit" value="submit" />
</form>
<form action="WriteTxt6.php" method="post"
enctype="multipart/form-data">
<label for="file">Input Text for artical Six:</label>
<textarea rows="25" cols="100" name="content">
</textarea>
<br />
<input type="submit" name="submit" value="Save" />
</form>
</td>
</tr>
</body>
</html>
trying to rename the files uploaded based on which form they come from on the html page afile,bfile etc.
I have tried this php file a number of different ways. I can get it to work when I eliminate the large IF statement and make 6 separate files for uploading but I was hoping to make it one file
This is what I believe makes it fail moving the global variables in to normal vars?
<?php
//This function separates the extension from the rest of the file name and returns it
function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
//This if statement assigns the new file name to a variable and displays a message.
if (file_exists($_FILES["afile"]["tmp_name"]))
{
$new = "file1.";
$type = $_FILES ["afile"] ["type"];
$size = $_FILES ["afile"] ["size"];
$error = $_FILES ["afile"] ["error"];
$name = $_FILES ["afile"] ["name"];
$tmpname = $_FILES["afile"]["tmp_name"];
$names = "afile";
echo "file1 Uploaded <br />";
}
elseif (file_exists($_FILES["bfile"]["tmp_name"]))
{
$new = "file2.";
$type = $_FILES ["bfile"] ["type"];
$size = $_FILES ["bfile"] ["size"];
$error = $_FILES ["bfile"] ["error"];
$name = $_FILES ["bfile"] ["name"];
$tmpname = $_FILES["bfile"]["tmp_name"];
$names = "bfile";
echo "file2 Uploaded <br />";
}
elseif (file_exists($_FILES["cfile"]["tmp_name"]))
{
$new = "file3.";
$type = $_FILES ["cfile"] ["type"];
$size = $_FILES ["cfile"] ["size"];
$error = $_FILES ["cfile"] ["error"];
$name = $_FILES ["cfile"] ["name"];
$tmpname = $_FILES["cfile"]["tmp_name"];
$names = "cfile";
echo "file3 Uploaded <br />";
}
elseif (file_exists($_FILES["dfile"]["tmp_name"]))
{
$new = "file4.";
$type = $_FILES ["dfile"] ["type"];
$size = $_FILES ["dfile"] ["size"];
$error = $_FILES ["dfile"] ["error"];
$name = $_FILES ["dfile"] ["name"];
$tmpname = $_FILES["dfile"]["tmp_name"];
$names = "dfile";
echo "file4 Uploaded <br />";
}
elseif (file_exists($_FILES["efile"]["tmp_name"]))
{
$new = "file5.";
$type = $_FILES ["efile"] ["type"];
$size = $_FILES ["efile"] ["size"];
$error = $_FILES ["efile"] ["error"];
$name = $_FILES ["efile"] ["name"];
$tmpname = $_FILES["efile"]["tmp_name"];
$names = "efile";
echo "file5 Uploaded <br />";
}
elseif (file_exists($_FILES["ffile"]["tmp_name"]))
{
$new = "file6.";
$type = $_FILES ["ffile"] ["type"];
$size = $_FILES ["ffile"] ["size"];
$error = $_FILES ["ffile"] ["error"];
$name = $_FILES ["ffile"] ["name"];
$tmpname = $_FILES["ffile"]["tmp_name"];
$names = "ffile";
echo "file6 Uploaded <br />";
}
//This applies the function to our file
$ext = findexts ($_FILES ['$names'] ['name']) ;
//This assigns the subdirectory you want to save into.
$targett = "forms/upload/";
//This combines the directory, the new file name, and the extension
$target = $targett . $new.$ext;
// makes sure image meets specs
if ((($type == "image/gif")
|| ($type == "image/jpeg")
|| ($type == "image/pjpeg"))
&& ($size < 2000000))
{
if ( $error > 0)
{
echo "Return Code: " . $error . "<br />";
}
else
{
echo "Thank You! <br />";
}
//saves uploaded file
if (file_exists("forms/upload/" . $name))
{
move_uploaded_file( $tmpname, $target);
echo $name . " Old File Over Written. ";
}
else
{
move_uploaded_file( $tmpname, $target);
echo "Stored in: " . "forms/upload/" . $name;
}
?>
Maybe is my elseif statement broken?
I have only been playing with this php stuff for about a week so if I'm way off mark here sorry for wasting your time.
Maybe if I try making 6 functions with the globals move_uploaded_file($_FILES["file"]["tmp_name"], $target); than placeing them in the elseif statement it could work?
this is a copy of the working php page
<?php
//This function separates the extension from the rest of the file name and returns it
function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
//This applies the function to our file
$ext = findexts ($_FILES['afile']['name']) ;
//This line assigns a random number to a variable. You could also use a timestamp here if you prefer.
$new = "file1.";
//This assigns the subdirectory you want to save into... make sure it exists!
$targett = "forms/upload/";
//This combines the directory, the random file name, and the extension
$target = $targett . $new.$ext;
if ((($_FILES["afile"]["type"] == "image/gif")
|| ($_FILES["afile"]["type"] == "image/jpeg")
|| ($_FILES["afile"]["type"] == "image/pjpeg"))
&& ($_FILES["afile"]["size"] < 2000000))
{
if ($_FILES["afile"]["error"] > 0)
{
echo "Return Code: " . $_FILES["afile"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["afile"]["name"] . "<br />";
echo "Type: " . $_FILES["afile"]["type"] . "<br />";
echo "Size: " . ($_FILES["afile"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["afile"]["tmp_name"] . "<br />";
if (file_exists("forms/upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["afile"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["afile"]["tmp_name"], $target);
echo "Stored in: " . "forms/upload/" . $_FILES["afile"]["name"];
}
else
{
echo "Invalid file";
}
That is a heck of a lot of repeated code! Do you really need 6 separate HTML forms? Why not have one form, with a dropdown for choosing the event?
<select name="event">
<option value="afile">Event One</option>
<option value="bfile">Event Two</option>
...etc
</select>
For the input, use:
<input type="file" name="userfile" id="file" />
Then instead of your lengthy if/else clauses, simply do something like this:
if ( isset($_POST['event']) && file_exists($_FILES['userfile']['tmp_name']) )
{
// base this code on the value of $_POST['event']
}
Maybe you should use
isset($_FILES["afile"])
instead of
file_exists($_FILES["afile"]["tmp_name"])
--edit
to answer your question in the title: it is possible, if, e.g $_FILES["afile"], is set. That way you'll have an array in your new variable.
Very close to working actually.
//Uses $names as a key in $_FILES - no single quotes for variable expansion....
$ext = findexts($_FILES[$names]['name']);
Might I also suggest using some sort of loop instead of a big elseif with a bunch of pasted code. Two improvements used: foreach on the $_FILES array, and extract
foreach ($_FILES as $fileKey => $values)
{
// Sets $type, $size, $name, $tmp_name, $error
extract($values);
//This applies the function to our file
$ext = findexts($name) ;
//This assigns the subdirectory you want to save into.
$targett = "forms/upload/";
//This combines the directory, the new file name, and the extension
$target = $targett . $new.$ext;
// makes sure image meets specs
if ((($type == "image/gif")
|| ($type == "image/jpeg")
|| ($type == "image/pjpeg"))
&& ($size < 2000000))
{
if ($error > 0)
{
echo "Return Code: " . $error . "<br />";
}
else
{
echo "Thank You! <br />";
}
//saves uploaded file
if (file_exists("forms/upload/" . $name))
{
move_uploaded_file( $tmp_name, $target);
echo $name . " Old File Over Written. ";
}
else
{
move_uploaded_file( $tmp_name, $target);
echo "Stored in: " . "forms/upload/" . $name;
}
}
}
I just wanted to thank everyone who left answers to my questions I was quite amazed at the short response time to my question. After trying and testing your answers I was still coming up short of any type of real solution to my problem it wasn't till I discovered that I forgot to end a if statement that I started making progress towards a answer.
if ((($type == "image/gif")
|| ($type == "image/jpeg")
|| ($type == "image/pjpeg"))
&& ($size < 2000000))
{
Now that this if statement has a ending and I have had time to study all of your answers I'm sure I will have this page working to my liking in no time.
It's working now but their are still refinements to be made. Here is my working html and php pages.
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<!--
html admin page for HH
-->
<title>Administartor Panel</title>
<head>
<link rel="stylesheet" type="text/css" href="admin.css" />
</head>
<body>
<h1>Heritage House Administartor Panel</h1>
<br/>
<table>
<tr>
<td id="eone">
<br/>
<p>Event Uploader</P>
<form action="formupload.php" method="post"
enctype="multipart/form-data">
<label for="selection">Please select the event you would like to edit.</label>
<select name="selection">
<option value="file1">Event1</option>
<option value="file2">Event2</option>
<option value="file3">Event3</option>
<option value="file4">Event4</option>
<option value="file5">Event5</option>
<option value="file6">Event6</option>
</select>
<br />
<p> Please upload your event picture file here.</p>
<p align="center">
<label for="file">Photo:</label>
<input type="file" name="file" id="file" />
<input type="submit" name="submit" value="Upload" />
</p>
</form>
<form action="WriteTxt1.php" method="post"
enctype="multipart/form-data">
<label for="file">Input Text for the event artical:</label>
<textarea rows="25" cols="100" name="content">
</textarea>
<br />
<input type="submit" name="submit" value="Save" />
</form>
</td>
</tr>
</form>
</td>
</tr>
</body>
</html>
PHP
//This function separates the extension from the rest of the file name and returns it
function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
//This if statement assigns the new file name to a variable from the value of the options menu in the html form selection.
$new = $_POST ["selection"] . ".";
//This applies the function to our file
$ext = findexts ($_FILES ["file"] ["name"]) ;
//This assigns the subdirectory you want to save into.
$targett = "forms/upload/";
//This combines the directory, the new file name, and the extension
$target = $targett . $new.$ext;
// makes sure image meets specs
if ((($_FILES ["file"] ["type"] == "image/gif")
|| ($_FILES ["file"] ["type"] == "image/jpeg")
|| ($_FILES ["file"] ["type"] == "image/pjpeg"))
&& ($_FILES ["file"] ["size"]< 2000000))
{
echo "file check pass<br />";
}
//makes an error if file does not meet standards
if ( $_FILES ["file"] ["error"] > 0)
{
echo "Return Code: " . $_FILES ["file"] ["error"] . "<br />";
}
else
{
echo "Thank You! <br />";
}
//saves uploaded file
if (file_exists("forms/upload/" . $_FILES ["file"] ["name"]))
{
move_uploaded_file( $_FILES ["file"]["tmp_name"], $target);
echo $_FILES ["file"] ["name"] . " Old File Over Written. ";
}
else
{
move_uploaded_file( $_FILES["file"]["tmp_name"], $target);
echo "Stored in: " . "forms/upload/" . $_FILES ["file"] ["name"];
}
?>
<html>
<!--
html to return user to admin page
-->
<title>
Upload Result
</title>
<head>
<p>
<br />
Back To Admin Page
</p>
</head>
<body>
<form action="">
<a href="adminarea.html">
<input type="button" value="Back" />
</a>
</form>
</body>
</html>
Note: I have only been using notepad2 and firefox to edit and error check my code. I'm guessing that their are better ways of debugging php out their right?