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
Related
I am having a problem in getting a image uploading to the server (first I convert it to .png and resize it):
In functions.php
function imageUploadedResize($source, $target,$nWidth, $nHeight)
{
$sourceImg = Imagecreatefromstring(file_get_contents($source));
if ($sourceImg === false)
{
return false;
}
$width = imagesx($sourceImg);
$height = imagesy($sourceImg);
$targetImg = imagecreatetruecolor($nWidth, $nHeight);
imagecopyresized($targetImg, $sourceImg, 0, 0, 0, 0, $nWidth,$nHeight,$width, $height);
imagedestroy($sourceImg);
imagepng($targetImg, $target);
imagedestroy($targetImg);
}
In uploadtoServer.php
if(isset($_POST["fileToUpload"]))
{
$target_dir = "img/data/";
$fn = $_FILES['fileToUpload']['tmp_name'];
$newFileName = mysqli_insert_id($link).".png";
header('Content-type: image/png');
imageUploadedResize($fn, $target_dir.$newFileName, 45,60);
}
If I change $fn to a static image like "https://pbs.twimg.com/profile_images/54789364/JPG-logo-highres.jpg" it works so I guess I am having a problem in $fn. Any thoughts what can it be?
Part 1:
Form input
As Johnathan mentioned, you're checking isset($_POST["fileToUpload"]) when a file upload form will supply the value in the $_FILES array. So you need to change your reference from $_POST to $_FILES otherwise it will always return false.
Edit: As your comment states you are getting 100% false, you should use a more specific qualifier such as if($_FILES['fileToUpload']['error'] == 0) .
Your HTML form submitting the data needs to be enctype="multipart/form-data" otherwise your server will not collect anything file shaped from your form. HTML forms also need to be set in the POST method.
Part 2
File storage:
Your references to file system storage for the image, such as $target_dir = "img/data/"; are relative, so that means that the value of $target_dir needs to be the child of the file that is calling that script block.
It would probably help you a lot to use Absolute file system references, typically (but not exclusively) using $_SERVER['DOCUMENT_ROOT']."/img/data";
This example will store data in the www.your-web-site.com/img/data location.
Part 3
Displaying the image.
You have a header setting content type to image/png but you do not actually output the image, instead saving it to the file reference mentioned above. If you want to output the image to the browser, you need to skip saving it so:
imagepng($targetImg, NULL);
This will then output the image in $targetImg straight out to the browser. At the moment you appear to be saving it but not displaying it, despite setting the image header which means that any output to browser is expected to be an image content.
Some systems have a restriction on freshly uploaded data and prevent that data being handled too freely, to get around this it is advisable to use move_upladed_file to save the file to a "proper" destination and then access it to output to the browser.
Further Notes:
You should be checking $_FILES['fileToUpload']['error'] before acting on the file, as this will tell you if a file is present. using isset is a bad (very ambiguous) idea on this as the array can still be set, even if the file has not been uploaded.
You should use PHP error logging. You should not use error supression #.
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.
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.
I have this code that has an image tag. The img src is equal to "https://graph.facebook.com/<?php echo $user_id; ?>/picture". Is there anyway that I could get that image file and upload it to my server using the move_uploaded_image function with php?
No, move_uploaded_image moves files that were uploaded in a POST request.
If you want to get an image from a URL you need to make an HTTP request for it, e.g. via cURL.
Using PHP's imagecreate and file_get_contents and file_put_contents, you can create a blank image, get the image from the remote URL, then replace the blank image you created.
Similar to,
// Create a blank image
$imPath = "path/to/your/desired/image.jpg";
$im = imagecreatetruecolor(180, 151);
imagejpeg($im,$imPath);
// Download their fb image
$url = 'https://graph.facebook.com/' . $user['username'] . '/picture?type=large';
$bytes = file_put_contents($imPath, file_get_contents($url));
there is no function like move_uploaded_image . correct function is move_uploaded_file.
no. you can not get image file by this function .
first argument of this function is temp file name which is uploaded through Input type = file.
and another argument is the location where you want to put file....
you should refer $_FILES of php manual for more information....
http://php.net/manual/en/function.move-uploaded-file.php
I am using library to upload multiple images in php .This library use flash file and a php file . All things are working but problem is that I want to run a query after each image is uploaded to store image detail in database . If i write query in php file after uploading code then It upload images but no error given and no image details are put in database.
Can any one help me that how I add image details in database after uploading image using flash uploader
This is my code
<?
extract($_GET);
$filename = $_FILES['Filedata']['name'];
$temp_name = $_FILES['Filedata']['tmp_name'];
$error = $_FILES['Filedata']['error'];
$size = $_FILES['Filedata']['size'];
/* NOTE: Some server setups might need you to use an absolute path to your "dropbox" folder
(as opposed to the relative one I've used below). Check your server configuration to get
the absolute path to your web directory*/
if(!$error){
copy($temp_name, '../dropbox/'.$filename);
$news_query="insert into tbl_news(img_id,headline,caption,news_catgory_id,shooting_date) Values('0000','News_Headline','News_Caption',
'New_Category','now()')";
mysql_query($news_query) or die(mysql_error());
}
?>
all code working but query data is not updated in database
May be it's a sql error...if your "shooting_date" field is numeric or date, you can't set it's value like this: 'now()', you must strip the quotes.