Extra Extension Added to Filename (PhP) - php

So after weeks of work, I have nearly perfected my image hosting service, however this is the only thing that I need to fix.
The below string just gets the filename, but I find that removing the [$i] on either end or removing certain parts of the string, the way the filename is handled will change.
Let's pretend that our file is named taco.png
$file_name[$i]=($_FILES['file']['name'][$i]);
This is what is currently working best. This results in taco.png.png, but I don't want the second .png, so I tried this.
$file_name=($_FILES['file']['name'][$i]);
After removing the first i, we get t.png, it just keeps the first letter, but the second .png is gone... yay?
$file_name[$i]=($_FILES['file']['name']);
Removing the second [$i] results in Array.png.
Anything else causes a syntax error.
Any ideas?

I think you have the following Html:
<input type="file" name="file[]" />
<input type="file" name="file[]" />
<input type="file" name="file[]" />
Then you should process it this way:
foreach ($_FILES['file']['name'] as $filename) {
//Work with $filename
}
Alternative you can do:
<input type="file" name="foo" />
<input type="file" name="bar" />
<input type="file" name="baz" />
And process it this way:
$fooFileName = $_FILES['foo']['name'];
$barFileName = $_FILES['bar']['name'];
$bazFileName = $_FILES['baz']['name'];

Related

upload image as file from URL

Using this script "PHP and Jquery image upload and crop": http://www.webmotionuk.com/php-jquery-image-upload-and-crop/ I want to upload file not from local, but from URL. Any ideas how I must to do it?
I'm trying to do this like that
<form name="photo" action="test.php" method="post">
<input type="hidden" name="zdj_tmp" value="E:\WebServ3\httpd\telebim\zdjecia\<? echo $select_zdjecie_2["id_4"]; ?>.jpg"/>
<input type="hidden" name="zdj_name" value="<? echo $select_zdjecie_2["id_4"]; ?>.jpg"/>
<input type="hidden" name="zdj_size" value="7340043"/>
<input type="hidden" name="zdj_type" value="image/jpeg"/>
<input type="submit" name="upload" value="Upload" />
</form>
And than in php file
$userfile_name = $_POST['zdj_name'];
$userfile_tmp = $_POST['zdj_tmp'];
$userfile_size = $_POST['zdj_size'];
$userfile_type = $_POST['zdj_type'];
But it doesn't work.
Any ideas?
Try this.
<form action="test.php" method="POST">
<input type="text" name="fileURL" placeholder="URL to image">
<input type="submit" name="upload" value="Upload files"/>
</form>
Here is the PHP
//Get the file from the URL.
$content = file_get_contents($_POST['fileURL']);
//Open the file that exists in your root already
$fp = fopen("/location/to/save/image.jpg", "w");
//Copy the information into the existing image
fwrite($fp, $content);
fclose($fp);
This takes the information from the image, and copies (overwrites) it into your existing image, which is, well, just some useless image.
You can then load the image from your roots.
It's not the best way, since your original image will be messed up, and is only usable once, since other users will change it, and there are other better ways. But if your site is a very not-popular kind of site, and you just want to test some methods out, this is definitely one kind of way. Just not the best.

move_uploaded_file returns false with correct path and permissions

So I'm making this file upload system in PHP. The $target_path is the absolute path and the permissions of the folder it will be uploaded into is set to 777. The tmp_name also returns something valid and the $_FILES['file_uploaded'] is an array.
However, when I run the following line of code. It returns false.
move_uploaded_file($_FILES['file_uploaded']['tmp_name'], $target_path)
Am I missing something here?
Update [Form HTML code]
<form action="photos.php" enctype="multipart/form-data" method="POST">
<input type ="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size; ?>" />
<input type ="hidden" name ="upload" value="upload" />
<input type="file" name="file_upload" />
<input type="text" name="tags" value="" />
<input type="radio" name="size" value="_small" checked> Small<br>
<input type="radio" name="size" value="_medium"> Medium<br>
<input type="radio" name="size" value="_large"> Large<br>
<select name="imagelib_id">
<option value="1">General</option>
</select>
<input type="submit" name="submit" value="Upload" />
Solution
I missed a filename for the uploaded file. The following line fixed my problem:
move_uploaded_file($_FILES['file_uploaded']['tmp_name'], $target_path . $_FILES['file_uploaded']['name']);
As it says in the PHP manual entry, the second parameter of the needs to be a filename, not a directory. You can follow the sample code and use the "name" value of the array:
// FIXME: verify filename - see below
$destination = $target_path . $_FILES['file_uploaded']['name'];
move_uploaded_file($_FILES['file_uploaded']['tmp_name'], $destination);
I strongly recommend to follow the tips in this comment on the PHP manual and verify both the filename for dangerous characters and correct file extension:
// snippet taken from the comment by Yousef Ismaeil Cliprz
function check_file_uploaded_name ($filename) {
(bool) ((preg_match("`^[-0-9A-Z_\.]+$`i",$filename)) ? true : false);
}

$_FILES Array Name Assignment

I couldn't find any duplicate questions but apologies if there is one.
For multiple PHP file uploads, is there anyway of naming the array indexes.
E.g.
<input type='file' name='file['file1']'>
So, instead of collecting data via this:
$_FILES['file'][0]
You get it via this:
$_FILES['file']['file1']
The PHP manual has several examples of this on the relevant page; it goes on to explain:
PHP also understands arrays in the context of form variables (see the related faq).
The "related FAQ" says:
It's also possible to assign specific keys to your arrays:
<input name="AnotherArray[]" />
<input name="AnotherArray[]" />
<input name="AnotherArray[email]" />
<input name="AnotherArray[phone]" />
The AnotherArray array will now contain the keys 0, 1, email and phone.
We can see here that, by analogy, you should name your file field file[file1]. So:
<input type="file"
name="file[file1]"
title="A description of the field should go in here"
/>
Use the documentation. It is there to help you.
HTML:
<input type='file' name='file[file1]' />
<input type='file' name='file[file2]' />
<input type='file' name='file[file3]' />
PHP:
<?php
$_FILES['file'][file1];
$_FILES['file'][file2];
$_FILES['file'][file3];
// and so on...
there are 2 ways of doing this:
if HTML is:
<input type='file' name='file[file1]' />
<input type='file' name='file[file2]' />
Then PHP should be:
$_REQUEST['file']['file1'];
$_REQUEST['file']['file2'];
if HTML is:
<input type='file' name='file[]' />
<input type='file' name='file[]' />
Then PHP should be:
$_REQUEST['file'][0];
$_REQUEST['file'][1];
For more information kindly read the documentation : this new one and this old one too

upload multiple photos

I have a form which I am using to upload users photos but the problem is that I can upload 1 photos each time .like facebook I want my users to select multiple images in one shot. can anyone please guide. here is my present code.
<tr><td><input type="file" name="photos[]" /></td><td><input type="text" size="35" name="descriptions[]" /></td></tr>
and php is processing the uploaded images. can you please tell how should I do so that multiple images can be selected and uploaded in one shot
first thing is you need to make your form multipart
<form method="post" action="where_ever" enctype="multipart/form-data">
And if you use HTML5 the next part is to create a named array
<input type="file" accept='image/*' name="multiImageUpload[]" id="multiImageUpload" />
<input type="file" accept='image/*' name="multiImageUpload[]" id="multiImageUpload" />
<input type="file" accept='image/*' name="multiImageUpload[]" id="multiImageUpload" />
This will put all files into a $_POST array called multiImageUpload.
In order to allow a name to accept multiples in an array you need to use []
at the back of name, name[] or images[] or files[], also using the HTML5
property multiple multiple='' or multiple='multiple' will allow you
to select multiple files at once form a single input.
Here's some working sample code to play around with
The HTML
<form method="post" action="upload.php" enctype="multipart/form-data">
<input name='uploads[]' type="file" multiple=""/>
<input type="submit" value="Send">
</form>
The PHP
foreach ($_FILES['uploads']['name'] as $filename) {
echo '<li>' . $filename . '</li>';
}
// full contents of $_FILS
echo '<pre>';
var_export($_FILES);
echo '</pre>';

PHP Uploading: Uploading multiple files?

I'm currently uploading a single file successfully with the following form:
<html>
<body>
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
And with the following script:
<?php
error_reporting(E_ALL);
if (($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
$moved = move_uploaded_file($_FILES["file"]["tmp_name"], "C:/inetpub/wwwroot/PHP_Ramp/upload/" . $_FILES["file"]["name"]);
if ($moved) {
echo "Move: Success <br/>";
}
else {
echo "Move Failed <br/>";
}
echo "Stored in: " . "C:/inetpub/wwwroot/PHP_Ramp/upload/" . $_FILES["file"]["name"];
}
}
else
{
echo "Invalid file";
}
?>
I'm now trying to allow the user to select three different files from the form. I've found a few guides that show how to do something similar, but I can't quite get it working.
I've modified the form as follows (to include three inputs):
<html>
<body>
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<input type="file" name="file" id="file" />
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
But I'm not sure how to modify the php to handle all three files. I know I need to iterate through _FILES but everything I've tried isn't working. Any pointers would be appreciated.
Each file element must have a unique name, or use PHP's array shorthand:
<input type="file" name="file1" />
<input type="file" name="file2" />
or
<input type="file" name="file[]" />
<input type="file" name="file[]" />
Remember - the name attribute defines how you'll identify that field on the server, and if you use the same name multiple times, PHP will overwrite previous copies with the latest one as it parses through the submitted data. The array notation ([]) tells PHP that you INTENDED to have multiple fields with the same name, and that each copy it finds should be added to an array, not overwritten.
For the unique name version, you'd handle each as you are right now with the single file.
For the array version, PHP has a design stupidity that requires slightly different handling. You end up with a $_FILES array that looks like
$_FILES = array(
'fieldname' => array(
'name' => array(
0 => 'first file',
1 => 'second file',
etc...
)
)
)
You need to change
<input name="file" />
to
<input name="file[]" />
To make them into an array.
Then in your script, you reference them as:
$_FILES['file']['name'][0]; // first file
$_FILES['file']['name'][1]; // second file
Note you can replace name with any of the other file properties that you usually would use on a single file (e.g. size, type etc).
Alternatively, you can give them all different names:
<input name="firstfile" />
<input name="secondfile" />
Then in your script:
$_FILES['firstfile']; // first file
$_FILES['secondfile']; // second file
You can select multiple file in single input like this..
<input type="file" name="pic[]" id="pic" accept="image/*" multiple="multiple"/>
This input box can accept multiple files by pressing control key. Now you can access them in php like this
$_FILES['file']['name'][0]; // first file
$_FILES['file']['name'][1]; // second file
KISS Code with some explanations:
There is fully functional simple code below to get you started. You can add error checking, max size stuff etc after you get something simple working.
The $_FILES array is a 3 dimensional array built into PHP, and that's where all the uploaded file info is stored.
There will only be ONE element for each 'name=' part of the HTML INPUT tag.
So if your INPUT tag looks like this:
<INPUT name="InpFile[]" type="file" />
The $_FILES array will have one top array element named 'InpFile'.
Also notice the [] after the InpFile.....that tells PHP that each time you use an input tag with that same 'name' it will add it to that same $_FILES array top element.
Within that one element there are 5 other array elements with the names: 'name', 'type', 'tmp_name', 'error', and 'size'.
And each one of those array elements will contain the data for each file that is uploaded.
Your first uploaded file name will be in $_FILES ['InpFile']['name']['0']
And the other info about your first uploaded file will also be in the array the same way, for example the size of the first file will be in $_FILES ['InpFile']['size']['0']
Each subsequent file name will be in $_FILES ['InpFile']['name'][1], $_FILES ['InpFile']['name'][2]....etc
After your upload, each file will have a random temporary name in the $_FILES ['InpFile']['tmp_name'][0...n] element, which is the name that it uses to first upload the files to a temporary area.
SO, after you upload, you have to move the files from the temporary area to where you want them.
That is done with this statement:
move_uploaded_file($_FILES['InpFile']['tmp_name'][$Key],
$_FILES['InpFile']['name'][$Key] )
or die("Move from Temp area failed");
In the foreach statement below, $Key and $Name are only there so that $Key will get assigned increasing numbers for each iteration...i.e. 0, 1, 2.....etc, and that you can then use $Key to reference the name, tmp_name, etc of each file in the array.
This code lets you do it all on one page, since the form actually calls itself (action="") to post. So the first time the page is loaded, you would get an error in the php code because $_FILES hasn't been set yet......so all the code is in an If statement: If ($_FILES).
After you submit the form, it will do it's thing and then echo a status for each file after it's been moved to your area.
Also, this code will upload the file to the same directory the page is in...you can change all that using info from other posts on SO.
<FORM action="" method="post" enctype="multipart/form-data">
<P align="center"><B>Choose Files</B><BR>
<BR>
File One:
<INPUT name="InpFile[]" type="file" />
<BR>
<BR>
File Two:
<INPUT name="InpFile[]" type="file" />
<BR>
</P>
<P align="center"><BR>
<INPUT type="submit" name="submit" value="UpLoad">
</P>
</FORM>
<H3 align="center"> </H3>
<H3 align="center">Status:</H3>
<P align="center"> </P>
<P align="center">
Put this PHP Code right here on the same page:
<?php
If ($_FILES) {
foreach ($_FILES ['InpFile']['name'] as $Key => $Name) {
move_uploaded_file(
$_FILES['InpFile']['tmp_name'][$Key],
$_FILES['InpFile']['name'][$Key]
) or die("Move from Temp area Failed");
$EFileName = $_FILES['InpFile']['name'][$Key];
echo "<P>$EFileName: Uploaded";
}
}
?>
You need to change the value of the name attribute in the HTML side to file[] and then in PHP just iterate through $_FILES['files'] and process each element as you normally would with a single file.

Categories