Simple Image Upload Issue? - php

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

Related

how can send array in html forms to php

I want to send uploaded files address from html to php with array and then moved the uploaded files to images from tmp and checked their size
<form method="post" enctype="multipart/form-data" name="form1" id="form1" action="upload.php">
<p>
<input type="file" name="Img[]" />
</p>
<p>
<input type="file" name="Img[]" />
</p>
<p>
<input type="file" name="Img[]" />
</p>
<p>
<input type="submit" name="submit" id="submit" value="Submit">
</p>
</form>
<?php
$name=rand('0123456789',5).'jpg';
$files=array($_POST['Img[]']);
echo '<pre>';
print_r($files);
if($_FILES['$files[0]']['type'] == 'image/pjpeg'){
move_uploaded_file($_FILES[$files[]]['tmp_name'],'image/'.$name);
echo "success";
}
?>
I want to upload files with form
Input file elements never store in the $_POST array. You have to loop through the $_FILES array to get the file elements
if(isset($_POST['submit']))
{
for ($i=0; $i < count($_FILES['Img']['tmp_name']) ; $i++) {
$name=rand('0123456789',5);
if($_FILES['Img']['type'][$i] == 'image/jpeg')
{
move_uploaded_file($_FILES['Img']['tmp_name'][$i],'image/'.$name . '.jpg');
}
echo "success";
}
}
In this solution you can increase the number of input files with the same name as Img
Try this
<?php
if(isset($_POST['submit']))
{
foreach($_FILES['Img'] as $Img) {
$name=rand('0123456789',5);
if($Img['type'] == 'image/jpeg')
{
move_uploaded_file($Img['tmp_name'],'image/'.$name . '.jpg');
}
echo "success";
}
}
?>

Upload multiple files with a php script

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>

Multiple file upload herror

I found a nice tutorial on YouTube by Anthoniraj Amalanathan. On the video tutorial, it works fine for hem but when I try to replicate it, I get an error. Here is the code:
<form action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post">
<input type="file" name="upload[]">
<input type="file" name="upload[]">
<input type="submit" name="send" value="Send Now">
</form>
<?php
if(isset($_FILES['upload'])=== true)
{
$files = $_FILES['upload'];
for($x=0;$x<count($files['name']);$x++)
{
$name=$files['name'][$x];
$tmp_name = $file['tmp_name'][$x];
move_uploaded_file($files,'test/'.$name);
echo 'Upload OK';
}
}
?>
The message states that the error is on line 12 ($tmp_name = $file['tmp_name'][$x];) but I don't seem to figure out why.
Can some one help here?
Try this, I tested it and it works for me.
<form action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post">
<input type="file" name="upload[]">
<input type="file" name="upload[]">
<input type="submit" name="send" value="Send Now">
</form>
<?php
if(isset($_FILES['upload'])=== true) {
$files = $_FILES['upload'];
for($x=0;$x<count($files['name']);$x++) {
$name = $files['name'][$x];
$tmp_name = $file['tmp_name'][$x];
move_uploaded_file($files['tmp_name'][$x],'test/'.$name);
echo 'Upload OK';
}
}
?>
The error I got came from using an array as the temp. file location. By changing it to $files['tmp_name'][$x], it worked.
Old: move_uploaded_file($files,'test/'.$name);
New: move_uploaded_file($files['tmp_name'][$x],'test/'.$name);
Its just a typo. $file is never declared, it should be $files.
Here:
$tmp_name = $file['tmp_name'][$x];
// ^ missing s
Also here:
move_uploaded_file($files,'test/'.$name);
// ^^^^^^ shouldn't this be $tmp_name?
Try like this :
<?php
if(is_uploaded_file($_FILES['upload']['tmp_name'])){
foreach($_FILES['upload']['name'] as $x=>$name) {
$name = basename($_FILES['upload']['name'][$x});
$folder = 'test/';
$full_path = $folder.$name ;
if(move_uploaded_file($_FILES['upload']['tmp_name'][$x], $full_path)) {
echo 'Upload OK';
} else {
echo 'Upload Failed';
}
}
}else{
echo 'Upload Not Received';
}
?>

Upload multiple images to a folder with each input having a different name

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++)

Troubleshooting file upload error code 4

I am trying to upload multiple files to my server. If I try to upload a single file, it works fine. but if I try more than one, it gives me an error code 4, even though it does print the name of all the files correctly. Nothing is uploaded. I have the input type set correctly. Can someone help me?
Choose Image: <input name="uploadedfile[]" type="file" multiple="true"/><br /><br /><br />
<input type="submit" value="Upload Image!" style="margin-left:100px;"/>
Below is the code:
$i=0;
foreach($_FILES['uploadedfile']['name'] as $f)
{
$file['name'] = $_FILES['uploadedfile']['name'][$i];
$file['type'] = $_FILES['uploadedfile']['type'][$i];
$file['tmp_name'] = $_FILES['uploadedfile']['tmp_name'][$i];
$file['error'] = $_FILES['uploadedfile']['error'][$i];
$file['size'] = $_FILES['uploadedfile']['size'][$i];
if ($file["error"] > 0)
{
echo "Error Code: " . $file["error"];
}
$target_path = "uploads/".basename($file["name"]);
if(move_uploaded_file($file["tmp_name"], $target_path))
{
echo basename($file['name'])."<br />";
echo basename($file['tmp_name'])."<br />";
echo $target_path;
} else{
echo "There was an error uploading the file, please try again!";
}
$i++;
}
and my HTML form
<div id="album_slider">
<div style="text-align:center;margin:20px auto;font-size:27px;">Upload Image</div>
<br style="clear:both;font-size:0;line-height:0;height:0;"/>
<div style="width:700px;margin:auto;height:250px;text-align:left;">
<form enctype="multipart/form-data" action="uploader.php" method="POST" name="form">
Image Name: <input type="text" name="image_name" id="image_name"/><br /><br /><br />
<input type="hidden" name="a_id" id="a_id" value="<?php echo $a_id; ?>"/>
Choose Image: <input name="uploadedfile[]" type="file" multiple="true"/><br /><br /><br />
<input type="submit" value="Upload Image!" style="margin-left:100px;"/>
</form>
</div>
<br style="clear:both;font-size:0;line-height:0;height:1px;"/>
</div>
Try:
foreach ($_FILES["uploadedfile"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
echo "$error_codes[$error]";
move_uploaded_file(
$_FILES["uploadedfile"]["tmp_name"][$key],
$target_path
) or die("Problems with upload");
}
}
Well its going to fail as you want to upload MULTIPLE files, but your code is only designed to handle 1 single file.
Go ahead and read through how to handle multiple files here:
http://php.net/manual/en/features.file-upload.multiple.php
Use the condition check like below code
if(isset($_FILES["qImage"]) && !empty($_FILES["qImage"]["name"])){
$imgSubQuestion = $_FILES["qImage"];
}
if(isset($_FILES["sImage"]) && !empty($_FILES["sImage"]["name"])){
$imgSolution = $_FILES["sImage"];
}

Categories