i am uploading images /files on upload multiple image but it only shows image name not their mime type using java script
<input type='file' name='data[Expensedetail][description]["+i+"][expense_file][]' id='expense_file"+i+"' style='display: none' multiple='true'>
where 'i' is value of array coming in loop i = 0,1,2,3 ... everything works fine and i am getting this result on post
[images details][1]
[1]: http://i.stack.imgur.com/ixaXr.png and using
$finfo = finfo_open(FILEINFO_MIME_TYPE);
echo finfo_file($finfo, $file); //$file is the file name coming in above image in array
echo $file;
failed to open stream: No such file or directory
Sounds to me like you're missing enctype="multipart/form-data" in your form element - without it the server will process the file input as a text field and just receive the filename.
Related
I want to use a text-box, insert my file address is in the Desktop, and then upload it in the server, what should I do?
Is it because you dont like file uploader object how it looks?
If yes I suggest you to make it hidden but in background use its fuctionality.
Show file selected information in a textbox but keep the file into hidden file uploder object.
Another method to post files like audio and jpeg to server is to use Base64 format . Its consider as string but not recommanded for large files.
Then you need a backend function to save data into file object or simply insert data into database.
Do it this way:
$file_url= $_POST['file']; //Path of file to be uploaded
if file_exists($file_url):
$fh= fopen("NewfileName", a+);
fwrite($fh, file_get_contents($file_url));
fclose($fh);
endif;
I am trying to achieve two actions from an input file tag. I have the following input:
<input id='file-input' name='attach' type='file' style='margin-left:15px;'/>
This is found in messages.php. What I am trying to achieve are two things:
If the file uploaded is over 1mb in size (or is a file which is not an image), then produce a button which on click opens the save as menu, from where a user can select where they wish to download the data.
And secondly, as mentioned, if the file size is lower than 1mb, then simply display the data on the page (only works for image files).
I have other pages where I have used input type="file" to upload profile images, and have just displayed the image on the page. But I am unsure on how I can execute (1) - how I can open a menu from where the user can save the data?
Just serve the file with a Content-Disposition: attachment header, see PHP Outputting File Attachments with Headers
Among other nice answers I will do it this way.
I have made it possible by clicking on your suggested Download button, if file is greater than 1Mb you will get download pop up like this
Otherwise if file less than 1Mb it will just show it on browser will look like this:
Btw, the code is self explaining.
PHP part called index.php
<?php
$filename = "test3.jpg";
$maxSize = 1000000; // 1Mb
if (isset($_POST['save']))
fileHandler($filename, $maxSize);
function fileHandler($filename, $maxSize)
{
$fileinfo = getimagesize($filename);
$filesize = filesize($filename);
$fp = fopen($filename, "rb");
if ($fileinfo && $fp)
{
header("Content-Type: {$fileinfo['mime']}");
if ($filesize > $maxSize)
{
header('Content-Disposition: attachment; filename="NewName.jpg"');
}
fpassthru($fp);
exit;
} else
{
echo "Error! please contact administrator";
}
}
?>
HTML part inside index.php but it is important this code should comes after php tags and not before.
<form action="index.php" method="post">
<button type="submit" style="border: 0; background: transparent" name="save">
<img src="download.jpg" alt="submit" />
</button>
</form>
Note: it is important your php document start directly with <?php ..., please read
You won't need to fake a click or something. You probably need something like this.
User selects file and clicks the "Upload file" button.
File gets uploaded using e.g. PHP.
PHP displays the contents of the file, using the correct headers.
PHP determines if the file is smaller or larger than 1MB.
If the file is larger, set a header that forces the user to download the file (causing a select location popup).
If the file is smaller, do not set the header, causing the file to display in the browser.
Where Italic is a user action and Bold is a server action.
You can do this by getting the image mime type and setting the content disposition and content type headers:
$file = 'path/to/file';
if (file_exists($file)) {
$contents = file_get_contents($file);
$fileSize = filesize($file);
$image_info = getImageSize($file);
$mimeType = $image_info['mime'];
header("content-disposition: attachment; filename=" . basename($file));
header("content-type:$mimeType");
header("Content-length: $fileSize");
echo $contents;
exit;
}
I'm trying to store images on an FTP server to be used on other pages, but I'm getting multiple errors trying to get this work. Running everything on XAMPP. First I use input on an html page:
<input type="file" name="image" required>
Then I bring it over and try to upload it:
$image = $_POST["image"];
$ftpCon = ftp_connect("127.0.0.1", "21") or die("Could not connect to FTP");
ftp_fput($ftpCon, "image.png", $image, FTP_BINARY);
ftp_close($ftpCon);
With this code I get this error: " ftp_fput() expects parameter 3 to be resource, string given"
Change the line
$image = $_POST["image"];
to
$image = $_FILES['image']['tmp_name'];
When you upload items (files) via a form it's populated in the $_FILES superglobal.
An associative array of items uploaded to the current script via the
HTTP POST method.
http://se2.php.net/manual/en/reserved.variables.files.php
Make sure you also set the form with enctype='multipart/form-data'
So the first line of the PHP has to be changed to:
$image = $_FILES['image']['tmp_name'];
The $_FILES is associtaive and contains the following data:
(userfile = image, in your case)
$_FILES['userfile']['name']
The original name of the file on the client machine.
$_FILES['userfile']['type']
The mime type of the file, if the browser provided this information. An example would be "image/gif". This mime type is however not checked on the PHP side and therefore don't take its value for granted.
$_FILES['userfile']['size']
The size, in bytes, of the uploaded file.
$_FILES['userfile']['tmp_name']
The temporary filename of the file in which the uploaded file was stored on the server.
$_FILES['userfile']['error']
The error code associated with this file upload. This element was added in PHP 4.2.0
How can I save image with PHP which was uploaded with http post using FLASH?
To upload to i'm PHP using this:
var upload_to:*=new flash.net.URLRequest("url");
fileHandler.upload(upload_to);
And when I print $_FILES in PHP I get:
{"Filedata":{"name":"IMG_8658 copy44.jpg","type":"application\/octet- stream","tmp_name":"C:\\WINDOWS\\Temp\\php35.tmp","error":0,"size":183174}}
so the question is, how to form a file from that $_FILES variable?: ) Thanks
PHP doesn't store the file in memory. It's written out to a temporary file, which you can retrieve the name/path of from the tmp_name value (C:\WINDOWS...). The name field is the filename as provided by the client (IMG_8658...);
In your case, that'd be
$_FILES['Filedata']['tmp_name'] <-- location of temporary file
$_FILES['Filedata']['name'] <---original filename
$_FILES['Filedata']['size'] <--- size in bytes
$_FILES['Filedata']['type'] <-- mime type, as provided by the uploader
$_FILES['Filedata']['error'] <--- error code of upload operation (0 = a-ok)
use copy($_FILES['Filedata']['tmp_name'],'your destination path'); function.
This is pushan once again .I have a applied a code to download multiple pictures after selecting them through check boxes. the value property of the check box contains the full path of the image .The image file is not downloading here is the code snippet:
if(isset($_POST['picdnld'])) {
$picarry=$_POST['supplier_picture'];
foreach($picarry as $pic) {
$handle = fopen($pic, "r");
$filename1 = basename($pic);
$xt=pathinfo($pic, PATHINFO_EXTENSION);
$filename=$filename1;
echo $filename.'</br>';
$outhandle=fopen('image'."/".$filename,"w");
if($outhandle){
echo 'directory found'.'</br>';
} else {
echo "directory not found".'</br>';
}
while (!feof($handle)) {
$buffer=fread($handle,4096);
fputs($outhandle,$buffer);
}
}
fclose($handle);
fclose($outhandle);
}
supplier picture is the name of the checkbox whose post contains the image links.I found that $outhandle is returning false every time. please help me to down load multiple selected images.
This is not possible the way you show. A response can generally contain only one image resource.
The most common way of doing this is to put all images into a ZIP file, and offer that for download. You could use the ZipArchive class for that. The linked page contains a small example.
Just read the image file and before sending it to browser
set the content header before sending to browser
header ("Content-Type: image/jpeg").