numerous errors handling file upload in php - php

I'm using this code to process uploaded files:
mkdir("files/" . $id, 0700);
$path = "files/" . $id;
$count = 0;
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
foreach($_FILES['attachments']['name'] as $f => $name)
{
if($_FILES['attachments']['error'][$f] == 4)
{
continue;
}
if($_FILES['attachments']['error'][$f] == 0)
{
if(move_uploaded_file($_FILES["attachments"]["tmp_name"][$f], $path.$name))
{
$count++;
}
}
}
}
$id is a random number taken from the database. Besides, I'm using this markup:
<input type="file" id="attachments" name="attachments[]" multiple="multiple" accept="*">
While the exact same code had worked brilliantly before, it now throws numbers of errors I can't really deduce:
1: mkdir(): File exists in ... on line ... (<-- now, it doesn't for granted!)
2: Undefined index: attachments in ... on line ... (well, it's defined also using form method post!)
3: Invalid argument supplied for foreach() in ... on line ... (which is quite clear as the above stated errors do prevent the foreach from doing its job correctly)
Yes, I made sure that I'm actually using POST. I also tried changing the file input's name from attachments to any other, however, scenario remains the same.
Adding enctype="multipart/form-data" has done it.

1] Check for the folder rights 0777. Weather you are able to create directory or not
2] After posting form. Make sure your form has enctype = multipart/form-data tag.
In you file please check with
echo "<pre>";
print_r($_FILES);
exit;
If you are getting any data or not? If getting data then move ahead.

First check if your $id really contain something, secondly your form should have attribute of enctype = multipart/form-data for using input type file.
<form action="test.php" method="post" enctype="multipart/form-data">
Now in your case you will get the array of files you before your perform any work, see print_r of attachments:
echo "<pre>";
print_r($_FILES);
exit;

Related

Move_uploaded_file doens't work?

The problem I have is that my move_uploaded_file doesn't work.
I have the following code:
<input type="file" name="image<?=$a; ?>" />
$a is a variable to define each of the inputs because I have 4 ( I have them in a while)
The form has enctype="multipart/form-data"
The directory exists.
The following code is the php code I used to get the images
for($a=1; $a<=4; $a++){
$diretor = "../images/imprensa/".$_FILES['image'.$a]['name'];
$image = basename($_FILES['image'.$a]['tmp_name']);
if(move_uploaded_file($image, $diretor)){
echo "yey";
}else{
echo "Oh";
}
}
Already tried to print the variables and I get both temp_name and basename but the code doesn't seem to work
Where is the error here?
EDIT
The move_uploaded_file is INSIDE for, sorry for not clearing it out
Don't use basename() on the tmp_name parameter. The first argument to move_uploaded_file must be a tmp_name parameter. On the other hand, you should use it on name, since that comes from the user, and they might try to add other directory names.
You should also call it inside the loop, otherwise you're only moving the last file.
for($a=1; $a<=4; $a++){
if ($_FILES['image'.$a]['error'] == 0) {
$diretor = "../images/imprensa/".basename($_FILES['image'.$a]['name']);
$image = $_FILES['image'.$a]['tmp_name']);
if(move_uploaded_file($image, $diretor)){
echo "yey";
}else{
echo "Oh";
}
}
}

Having issues with PHP File Upload

I am trying to upload a file to my site but for some odd reason its not working. The exists=true is triggering so this means it should be working right? Its giving me the $error=true;. Below is my code:
HTML:
<input size="30" enctype="multipart/form-data" name="profile_icon" id="register-profile_icon" class="elgg-input-file" type="file">
PHP:
$profile_icon = $_FILES["profile_icon"];
if($profile_icon){
$exists=true;
}
$error = false;
if(empty($profile_icon["name"])){
register_error(elgg_echo("profile_manager:register_pre_check:missing", array("profile_icon")));
$error = true;
}
Use $_FILES["profile_icon"]["tmp_path"] for file path.
$_FILES["profile_icon"] This will contains arrays of values like name, tmp_path, type, size etc.
On another note you need to add enctype attribute to your <form>.
And there is function available for moving upload function move_uploaded_file();

php multiple file uploads get the exact count of files a user uploaded and not the count of all input fields in the array

Ok. I give up on this. When uploading multiple files to the server using php, what is a fail safe method to return the count of files the user has actually uploaded?
Whatever I have done so far, returns the count of all the fields in the form, and the count of the files a user uploads. So if the total fields in the form were 3 and a user uploaded only 2 files, I still get 3 as the count of file uploaded.
One place suggested using array_filter to do this, but that's totally beyond me.
echo count($_FILES['file']['tmp_name']); //3
echo count($_FILES['file']); //3
Any fail safe method you follow and can suggest other than looping through the FILES array size to check for this?
My form is structured like any other:
<form action="process.php" method="post" enctype="multipart/form-data">
<div><input type="file" name="file[]"></div>
<div><input type="file" name="file[]"></div>
<div><input type="file" name="file[]"></div>
<div><input type="submit"></div>
</form>
Exactly as they told you, just simply use array_filter().
echo count(array_filter($_FILES['file']['name']));
It will return the right number
$count = 0;
foreach($_FILES as $file) {
if(isset($file["file"]["tmp_name"]) && !empty($file["file"]["tmp_name"]))
$count++;
}
Try this
$count = 0;
$fileCount=count($_FILE['file']['name'];)
$fileSize=$_FILES['file']['size'];
for($i=0;$i<$ fileCount;$i++) {
if ($fileSize[$i] > 0) {
$count++;
}
}

PHP - Stop while loop after reading every single line from text file

I want the Code below to read individual line of text from dataFile.txt and show it in input field.
Problem is After reading first line from text document it shows all remaining lines of text from text file into input field. But on clicking submit it should show second line only then again on submitting it should show third line only, inside input field. please help.
<?php
$file = __DIR__."/dataFile.txt";
$f = fopen($file, "r");
$array1 = array();
<form action="datagGet.php" method="get">
<input type="text" value="
<?php while ( $line = fgets($f, 100) )
{
$nl = mb_strtolower($line);
echo $nl;
if(isset($_GET['done']))
{
$nl++;
}
else
{
break;
}
}
?>"
name="someText">
<input type="submit" name="done" >
</form>
You have several problems with you code. And the first comment above points to many of the. Key is the fact that the $_GET['done'] is set for the form submit and therefore you will echo all the lines of the output. It never breaks.
Also there is the fact that you are opening the file for reading each submit of the form. Although I don't see a simple way around this unless you store the file contents between requests.
One possible option is to use 'file()' to read the entire contents into an array. And then use sessions to store which line has been read. Then on each submit, look for the index of the array from the session read; advance it by one read the file again and return that line. Wow wasteful. But okay for simple site.
so use file to get the lines in an array.
output the first line into the value.
store the next index to be read in the $_SESSION variable like $_SESSION['next_line'] = 1
then upon further submissions. read it all back in. look up the 'next_line', and output that line.
so, for example
$array = file('your file name');
$output = $array[0];
if (isset($_SESSION['next_line']))
$_SESSION['next_line'] = intval($_SESSION['next_line']) + 1;
else
$_SESSION['next_line'] = 1;//prime the pump
echo the form with $output
then rinse and repeat. e.g. read, get output (next_line) with file, set $_session = next_line + 1; render output in form.
ps. some extra notes
* of course you'll need to start session on each request.
* you'll need to check if the $_SESSION['next_line'] is set. if not, set it to 1 (prime it)

Undefined index while uploading

I have a problem that i just quite do not understand at all. I have this uploading script that always return Notice: Undefined index: uploadPDF in xxxxx
I've made sure that the form has the enctype="multipart/form-data" <form action="" method="POST" enctype="multipart/form-data">
The field also has the same name that i ask for in the code <input name="uploadPDF" size="100" type="file" title=""/>
When i try to echo $_POST['uploadPDF'] i actually get the filename in question. But when i try to var_dump the following $_FILES['uploadPDF']['name'] i get the undefined index error.
I really cant see what the problems is. I'm running on a inhouse IIS server.
Debug information:
This is the "debug" i try to do:
echo $_POST['uploadPDF']."<br />";
$filename = $_FILES['uploadPDF']['name'];
var_dump($filename);
echo "<br />";
var_dump($_FILES);
This is the output i get:
TEST PDF PORTAL V3.pdf
Notice: Undefined index: uploadPDF in C:\inetpub\myfolder\V4\admin\addRoutine.php on line 29
NULL
array(0) { }
When you upload file, you should use
$_FILES['file_name'] not $_POST['file_name'] that is because, the file information is stored in the $_FILES arrays, since you have named your input type to 'file'
So, I would suggest
Changing
echo $_POST['uploadPDF'];
to
echo $_FILES['uploadPDF'];
Your form as you wrote it has no action specified.
( <form action="" method="POST" enctype="multipart/form-data"> )
You need the asign "path_to_yourform.php" as your form action.
You better write it like this:
echo $_POST['uploadPDF']."<br />";
$filename = $_FILES['uploadPDF']['name'];
echo var_dump($filename)."<br />";
Well, this is quite embarrassing, one of the other guys working on this had left a <form action="" method="post"> in one of the included files in the project. Since this form tag was before my form tag $_FILES did not catch the index because of the missing enctype in the first form tag!

Categories