Using uploaded file without saving to server disk - php

I have created a PHP script to upload a file, unfortunately I don't have permission to save files on the disk. I have to upload an excel file (using phpexcel), then I have to read all the rows in the file and save to disk, Is there any way for me to process this file without saving to disk, I tried to read $_FILES['file1']['tmp_name'] but it doesn't work.
could u please suggest a method to process this file
Thank you for the consideration

By "save to disk" you mean to send it back to the user for him to download it?
Usually, you shall have write access to (at least) the PHP temporary directory. Have you tried whether the form and script work in a local environment? Maybe there is something elso wrong with the upload?!
Finally: Why so you not have the persmission to save files? Are you allowed to create a subdirectory below you PHP file (via FTP) and give that one full permissions?

I tried to read $_FILES['file1']['tmp_name']
most probably you have just encountered an error.
that happens to beginner programmers very often
you have to repair that error instead of looking for odd workarounds.
Start from checking $_FILES['file1']['error']
what does
var_dump($_FILES['file1']['error']);
say?

Instead of sending your files with a form (multidata over HTTP POST), you can send your files with a little bit of Javascript with the HTTP PUT method to your server.
This scenario is described in the official documentation of PHP -> PUT method support.
Due some restrictions described in the documentation you have to do some workarounds to be able to work it properly.
You can read the direct input stream from your Webserver. The data will be piped from your Webserver to your PHP programm and will be only saved in memory.
To do a PUT Ajax call with jQuery was answered here. You can use a jQuery upload plugin like Uploadify.

Related

Process Uploaded file on web server without storing locally first?

I am trying to process the user uploaded file real time on the websever,
but it seems, APACHE invokes PHP, only once complete file is uploaded.
When i uploaded the file using CURL, and set
Transfer-Encoding : "Chunked"
I had some success, but can't do same thing via browser.
I used Dropzone.js but when i tried to set same header, it said Transfer -Encoding is an unsafe header, hence not setting it.
This answer explains what is the issue there.
Can't set Transfer-Encoding :"Chunked from Browser"
In a Nutshell problem is , when a user uploads the file to webserver, i want webserver to start processing it as soon as first byte is available.
by process i mean, PIPING it to a Named Pipe.
Dont want 500mb first getting uploaded to a server, then start processing it.
But with current Webserver (APACHE - PHP), I cant seem to be able to accomplish it.
could someone please explain, what technology stack or workarounds to use, so that i can upload the large file via browser and start processing it, as soon as first byte is available.
It is possible to use NodeJS/Multiparty to do that. Here they have an example of a direct upload to Amazon S3. This is the form, which sets content type to multipart/form-data. And here is the function for form parts processing. part parameter is of type ReadableStream, which will allow per-chunk processing of the input using data event.
More on readable streams in node js is here.
If you really want that (sorry don`t think thats a good idea) you should try looking for a FUSE Filesystem which does your job.
Maybe there is already one https://github.com/libfuse/libfuse/wiki/Filesystems
Or you should write your own.
But remember as soon as the upload is completed and the post script finishes his job the temp file will be deleted
you can upload file with html5 resumable upload tools (like Resumable.js) and process uploaded parts as soon as they received.
or as a workaround , you may find the path of uploaded file (usually in /tmp) and then write a background job to stream it to 3rd app. it may be harder.
there may be other solutions...

Processing file in the users directory

I am writing a scripts that processes the .csv file. The script currently have to upload the csv file to the server in order to process it, and the user have to download the processed file which is a lot of work from a user.
My question is, is there a way to process files from the user's directory path without the user having to upload the file first? So the user will just browse to the file to be processed and the file will be save and processed in that path.
Thanks,
Sbo
Then the only option you have is to do it client-side. To do it client-side you thus have to use a client-side technology like Flash or JavaScript. The latter is probably the better choice. The following URL explains how you can do a client-side file upload: http://igstan.ro/posts/2009-01-11-ajax-file-upload-with-pure-javascript.html
You want to get access to user's computer? Forget it.
Only way to achieve it is to use Java Applets with special permissions in php you need to upload it, it can be uploaded to temp directory but you need to still upload it.
Java Applets need to be signed and has certificate to be accepted by user. There is no other way I know to get access to user's files.
Check this link as well

How do PHP and Apache handle uploaded files?

I am working on some PHP upload code.
When I call the copy function it will start a thread in incremental mod. I want to read the full details of the upload functionality for apache and PHP. In fact, I want implementation details for this functionality, namely:
How a file is copied in temp folder
How it is copied to the proper destination
What happens when server is busy and client is sending fix size of chunks
I am trying to upload file from my Android application. I am using this code for uploading. But my most of the files header are changed. Most of my uploaded files' sizes increase. Why its happen? In fact I want to investigate it. My client is Android application and server is apache 2.0 with php 5.
Thanks in advance.
CodeCaster allready gave part of the answer...
Just a really big tip here, php.net has a huge database with ALL of the functions and great examples that goes with them.
This is the main page about handling file uploads (just a table of contents) http://nl3.php.net/manual/en/features.file-upload.php
Here is pretty much everything you need to know:
http://nl3.php.net/manual/en/features.file-upload.post-method.php
W3Schools also has a tutorial on this.
http://w3schools.com/php/php_file_upload.asp

Getting upload file size before upload

When the user selects a file to be uploaded, is there a way I can get the exact size of this file before the upload even begins? I'm guessing this needs to be done on the client side with jQuery or JavaScript. Any ideas how?
This cannot be done in pure Javascript in current browsers.
Instead, you can use Uploadify, which uses Flash.
In non-IE browsers, you can also use HTML5 to read files on the client.
$("#file_input_selector").bind('change', function(){
alert(this.files[0].size);
});
Not sure of all the compatibility issues, but this seems to be working just fine for me.
Take a look at this post:
http://forums.digitalpoint.com/showthread.php?t=6704
Javascript doesn't have the ability to check file sizes (or access the file system for that matter). You'll need to upload the file to get the size
I suggest you look at the HTML5 File API. This, combined with some JS might be able to help you. I only say might because I have not yet had a chance to browse at this part of the HTML5 standard.
http://www.w3.org/TR/FileAPI/#dfn-filereader
The way PHP file uploads work, it is very hard to check file details before, or during a file upload (since the file is uploaded before your code even gets loaded).
I know it is possible to do some fancy things in some other languages (possibly Perl or Python) that handle the file uploading directly with the script (where the script opens the socket and handles the whole transfer itself), however PHP does this for you and accepts any file on your script's behalf. The file gets discarded if it is not within PHP's acceptable limits, but only after the file is completely uploaded.
There have also been several file upload implementations made using Flash, but not being an ActionScript coder, I can't really help too much there either.

How to setup PHP to be a "pipe" for downloads from another location?

I'm wanting to setup a php script and host it on my server that will let me download files from other locations, but making it look like it's coming from my server. Maybe using curl or htacess. Also I was hoping that there would be a way to get around having my server deal with the bandwidth. Does that make sense? Is this doable?
-- Update
Kind of like a proxy, but without the file downloading to memory and then sending it to the client.
You can do this by simply passing the target url to you script, open the url with file_get_contents(), curl or other file functions and echo the data. ensure to set the Content-Type header to "application/octet-stream" to force the browser to save the file instead of displaying it.
As for the bandwidth: You'll have to deal with it. If your server downloads a file, it will use up the bandwidth. It will even use it up twice because it has to receive AND send the data.
I don't know why you mention htaccess, because that has nothing to do with your problem.
Also I was hoping that there would be a way to get around having my server deal with the bandwidth. Is this doable?
No.
I'd recommend setting up a linking system on your site like http://example.com/download.php?id=12 that would then forward directly to the remote file, that way you'd save on bandwidth and if someone look at the link on your page it would look like it coming from your server. It would still show the other site in the download manager but if your trying to save bandwidth it's a small price to pay.
Thanks for the help... I figured out what I was needing to do, I'm going to use mod_xsendfile. It lets you set an external source for where the file is located, and then lets the user download the file without knowing where the file actually is located.

Categories