using a locally stored version of an image - php

I have a page that the user can upload an image to and then move around and resize using jquery draggable and jquery resizable.
To save on mutilple requests to the server, would it be posible to use the locally stored version of the image to speed things up and then have a save button which would only upload the image to the server when requested.
I have tried to do this unsuccessfully as I am only getting the temp location of the file
<?
$posted=$_REQUEST['posted'];
if($posted!='')
{
$image = $_FILES['file']['tmp_name']."/".$_FILES['file']['name'];
?><img src="<? echo $image ?>" width="480" height="360" /><?
echo "posted=".$image;
}
?>
<form action="#" enctype="multipart/form-data" method="post">
<input type="hidden" name="posted" value="1" />
<input type="file" id="file" name="file" size="30" />
<input name="submit" type="submit" value="send" />
</form>
is this possible?

You have to move the uploaded file using move_uploaded_file() to the destination you want.

If you mean "resize the image on the user's computer in their browser and then upload once the cropping is done" then no.
As far as I am aware that's not possible unless you use Flash or a Java plugin for example.
The $_FILES['file']['tmp_name'] variable contains the temporary location of the uploaded file on the server. Temporary files are removed regularly, so to keep the uploaded file on the server you need to use move_uploaded_file().

according to the manual the super global $_FILES is
An associative array of items uploaded to the current script via the HTTP POST method.
So in order to use this global you have to have the images uploaded. I would say that you could avoid multiple requests to the server after you upload the image using jquery and using the move_uploaded_file() only after the user has chosen the size he wants.
Doing it the way you have suggested is not possible with only javascript and PHP. AS far as I know at least.

Related

Have file automatically upload to server when .php is launched

I'm trying to write code in php so that when the .php file is opened, it automatically uploads from a specific file address on my windows computer to the localhost server.
This is my attempt but I'm not sure I fully understand how to do this without using an HTML form where the user specifies the file they want to upload.
<?php
$target = 'UPLOADED_FILE.csv';
move_uploaded_file('C:\Users\Ken.Feier\Desktop\temp\REPORT.csv', $target);
?>
I want the code to take the REPORT.csv file from my personal computer and upload it to our server with the file name UPLOADED_FILE.csv
UPDATE: I see that my problem will not be solved with php. Can anyone recommend any other solution involving Filezilla or any other FTP that can be automated?
That's not how it should be done.
You need a page with a html form, which will send the data to server on submit. Note that the file could be stored on your personal.
Form code e.g.
<form method="post" action="destination.php" enctype="multipart/form-data">
<input type="file" name="filename" />
<input type="submit" value="upload" />
</form>
Then, on the server, you can use the $_FILES['filename'] which contains your file's infos. Note that when a file is uploaded to the server, it's stored in the tmp folder, which is temporary, so you have to move your file to a persistent directory with move_uploaded_file(); (move_uploaded_file Docs)
E.g:
<?php
$file = $_FILES['filename'];
move_uploaded_file($file['tmp_name'], '/new/destination/for/the/filename.php');

read contents of file forms php

I'm trying to create a local php script that lets the user select a file, then outputs the file contents. I don't want to save the file anywhere, just read its contents. I've been reading guides on file input types and this is the examples I'm seeing:
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
My question is, since I don't want to upload the file anywhere, just read it's contents (this script is personal and runs locally so I'm not worried about security), is there a way to extract the file contents from the file selected in the <input type="file"> without putting this in a form? I'm new to this stuff and want to make it as simple as possible to just read the file's content.
PHP is a server side language, whichs means you'd need to "upload" the file in order to process it, even if web server is local, the browser needs to send the file to the PHP script
Here is a guide for file uploads via POST:
http://php.net/manual/en/features.file-upload.post-method.php
In order to get the contents of the script in a variable, you'd have to read the temporary file after the file has been uploaded through a form's post method:
$tmp_file = $_FILES['fileToUpload']['tmp_name'];
$contents = file_get_contents($tmp_file);

File upload and send textbox data using AJAX HTTP POST

I have one HTML form where I enter the Text fields and finally upload a image file into the server.
(HTML file:<input type="file" name="filename"/ >)
I use Ajax technique and HTTP POST request to perform this task.
But I'm unable to upload file but can see the text filed values in database.I'm trying to upload image file into a folder using getimagesize($_FILES['filename']['tmp_name']and move_uploaded_file() to move from temp folder to specific folder.
FirePHP is showing the warning message as :getimagesize() [function.getimagesize]: Filename cannot be empty in my .php file on line 19
line 19 contains the getimagesize() statement.
Could anybody please let me know is it possible to upload a file using ajax technique?or any other better way to do this?
Any help is greatly appreciated.
I will give you three basic ways to work on AJAX-based file uploads..
1) Faking AJAX-based file uploads - You'd create an iframe on the page (that you can hide with CSS), you can target your form to POST to that iframe.
<form target='upload_target' id="file_upload_form" method="post" enctype="multipart/form-data" action="upload.php">
<input name="file" id="file" size="27" type="file" /><br />
<input type="submit" name="action" value="Upload" /><br />
<iframe id="upload_target" name="upload_target" src="" style="width:0;height:0;border:0px solid #fff;display: none;"></iframe>
</form>
2.) FILE API: if your browser supports it, you can use the sophisticated FILE API to do what I'll call a Pure AJAX file upload - https://developer.mozilla.org/en/using_files_from_web_applications
3) You can use existing JQuery plugins such as Ajax File Upload and Multiple File Upload. Please do look up more such from the JQuery site, evaluate for cross-browser compatibility and use.

Image hosting with multiple servers

I'm developing an image host and wish to have images uploaded to a separate server from my web content, eg: http://i1.mysite.com instead of http://mysite.com/uploads. But I'm having some trouble figuring out how to do that.
Say I have this form:
<form action="http://mysite.com/upload" method="post" accept-charset="utf-8" enctype="multipart/form-data">
<input type="file" name="image" id="file_upload" />
<input type="submit" value="Upload" id="upload_submit" />
</form>
That will send an image file to /upload, where I can validate the file and save it, but that will be on the same server as the website is hosted, rather than a dedicated storage server. How can I achieve what I want without having the images uploaded on the same server as my web site?
I could always do:
<form action="http://i1.mysite.com/upload" method="post" accept-charset="utf-8" enctype="multipart/form-data">
<input type="file" name="image" id="file_upload" />
<input type="submit" value="Upload" id="upload_submit" />
</form>
which would send the image file to another server, but then when the image upload is complete I'd be redirected to http:/i1.mysite.com/upload.
Anyone have any experience with this and can recommend a course of action? Thank you!
Don't upload to the image server. Such content-specific servers should be optimized for serving up content, and not have to deal with consuming content.
Let the upload form send to your main site's server. You can then use other protocols to transfer the uploaded file(s) to the image servers. rsynch, scp, etc... This way you have all your "control" code in one location, and don't have to worry about synching databases and whatnot between multiple servers - all the data is kept on your main server, and the image servers just passively spit out image data.
I would recommend decoupling these two ideas. First, upload the image to your servers and in a separate process (perhaps a scheduled cron) move the images to other server. You likely do not want the user waiting for two uploads to finish.
Like the others have said, what you're trying to do is not optimal. If you really want to continue to do this, I'd suggest having the form submit to a PHP script which then processes it and places the file where it needs to be and then saves whatever information to the database that is necessary. You'll need to evaluate the best protocol for the data transfer from one server to the other. You'll probably end up using Curl, which you can learn about here and here as well as the curl docs
You could upload the image to your image host, and have it redirect back to your website afterward. One way to do this would be to add hidden "success" and "failure" URL inputs to the form:
<form action="http://i1.mysite.com/upload" method="post" accept-charset="utf-8" enctype="multipart/form-data">
<input type="file" name="image" id="file_upload" />
<input type="submit" value="Upload" id="upload_submit" />
<input type="hidden" name="success" value="http://mysite.com/success" />
<input type="hidden" name="failure" value="http://mysite.com/failure" />
</form>
The upload script on your image host would then redirect to the supplied URL after a successful upload:
<?php
.. handle uploaded file ..
if ($success) {
header ('Location: ' . $_REQUEST ['success']) ;
}
else {
header ('Location: ' . $_REQUEST ['error'] . '?message=' . $message) ;
}
?>

HTML Upload Form will only upload files found in the directory of the PHP file

I have an image uploader that uses the imgur.com API and jQuery's .ajax() function to upload images to their servers. However, if I browse for an image using the <input type="file"/> element of the form, it will only be successful in uploading an image if the image file is found in the same directory as the page.php file that the form is found in (shown below). How can I allow the form to upload images from any directory on my computer?
page.php:
<form action="page.php" method="post">
<input type="file" name="doc" id="doc" /><br/>
<input type="image" src="go.gif" name="submit" id="submit" />
</form>
You've forgotten the enctype="multipart/form-data" attribute on your form tag, for one. Without that, file uploads generally don't work too well.
Beyond that, the server won't really care what directory you're uploading FROM, especially under PHP. The uploaded copy on the server is stored with temporary filename ($_FILES['file']['tmp_name']) that has absolutely nothing to do with the directory/filename on your computer.
Once it's on the server, you'll have to actually move that temporary file somewhere else, as PHP will auto-delete it once the script terminates and you've not handled it yourself. move_uploaded_file() is what's generally used to take of that process.
Perhaps this is the only folder with write-permissions.
I guess it is jquery that is doing the actual posting to http://imgur.com/api/upload as the form is just posting to itself, so my guess is that jquery / javascript can only read files in your web-space and not on your whole hard-drive.

Categories