I was having a problem with pasting huge amount of text in a text area in HTML as it causes delays and slow processing time. Then one of the responders here in Stackoverflow suggested I upload the text file instead.
Now I am having problems on just how to do that exactly. I've been researching for hours but couldn't find the answer I'm looking for. What I need is for the user to select the text file then PHP reads the contents of that file into a variable. The file contains server logs so it should be huge.
I've been researching and options like fopen() or file_get_contents() seem to only open specific files, I need it dynamic, the user must select the file himself. Also, I am confused if I want to upload the file into the server or just read it's contents without uploading. I maybe prefer just reading.
We run a team that checks the user log of any users having issues, we have a tool that downloads a text file of any selected user, we then need to read that log into our PHP website/tool.
What you're looking for, on the client side, is <input type="file" name="myfile"> which allows the user to browse for a file - of any type - and upload it to the server. After it's uploaded, you get access to it from PHP through the $_FILES structure. See more here.
You may try this
file_get_contents($_FILES['uploadedfile']['tmp_name']);
Store this result into one variable. Print this result. Check what will be shown.
But the condition is you should use html form. I think you know this basic concepts.
Related
I'm trying to pass files directly to an input type="filename" control element so I don't have to manually click [browse] and search for a file. I'm not trying to bypass any type of security or anything like that, i'm just trying to "imitate" the clicking of the browse putting and selecting an image without actually having to search for the file in the BROWSE FOR FILES dialog.
I'm wanting to do this directly through javascript.
Now some of you may be wondering "why the crap are you trying to do that?" well my employer has a website that he has to upload images of daily reports to and they use the standard
<input type="hidden" name="fileInput">
He wanted me to try and make a program that just does it for him and will just auto upload the files for him and all he has to do is hit submit at the end.
Is this possible?
EDIT#
I was thinking I needed to pass it the image in raw data:image/jpeg;base64 then pass it to the control somehow?
EDIT#2
What about doing something like
javascript:(function() { document.forms[0].file.value+='" + theFile + "';})()
Edit#3
From what I been looking into I guess the only possible thing is to try and do a straight HTTP Post with a MultiPart Entity. I guess this is the only way to go about doing it and its impossible to do anything directly from Javascript itself.
Would figure that since is considered a HTML object that javascript would be able to touch it. Kinda of weird that you can't.
This is similar to another question I answered recently. Basically, it is not possible. Even though you aren't trying to bypass security, it could be used to bypass security. Any and every time a webpage wants to access your files, the user must be alerted.
I am building a Discussion Forum as part of a bigger application I am building, the forum is just 1 section of the Application.
For my TextArea fields when posting a new Topic or a Post Reply, I have decided that nothing is as good as the PageDown Markdown Library. It is the same one that StackOverflow uses on all their sites and it works better than many of it's competitors.
The way the library ships though, I am not happy with the default Insert Image functionality. You hit the button to insert an image and it allows you to enter a URL for an Image and then it inserts the proper MarkDown syntax to show the linked image.
This just won't cut it. I need the functionality that you see on StackOverflow! Very similar anyways.
I need it to show a Dialog when you click the Insert Image button, like it does now, but instead of just an input field for a Image URL, it will have 2 filed options...
Upload image from your computer
Insert an Image URL and it will then DOWNLOAD the image from that URL and insert it into the post just as if you had uploaded it from your computer. This is important to not confuse this step. IT should not simply insert the Image linking it to the original Image URL. Instead it will take that URL and download/upload the Image to the same server that the upload from computer option does and then it will insert the NEW Image URL pointing to the newly uploaded image!
Based on some simple HTML like below for a Dialog window with a filed for my Upload from Computer functionality, which I already have working. I need to come up with some JavaScript and PHP that will download/save a remote image to my upload folder on my server when a button is clicked using only the URL that will be inside the URL text input field.
So it will need to do a few things...
Fetch and save an image file to my uploads folder using PHP when the only thing that the PHP function will receive is a URL of the image which could be on the same server or most likely a remote server.
After successfully saving/uploading an image from the URL, the PHP function will return a JSON string with the status/error and if successful then it will also return the actual URL and filename of where the new image is saved on the local server. The JavaScript/AJAX script will receive this JSON response and insert the Markdown syntax for the image into the PageDown editor.
The PHP function will need to ensure that the URL that it is trying to save/download is a valid image file and not some malicious file! Also not simply just some file of the wrong filetype like a non-image file unless we are allowing the file type.
It will be part of a module installed on many dinosaur servers so it needs to work on as many servers as possible too!
From the web
From your computer
I would be greatful of any help, tips, code snippets or anything to help with this. At this stage I really just need to build a nie PHP function that will upload images from a remote URL and also ensure that the URL passed in is a real image file or even better that it is in the allowed file types array!
A couple years ago I had started this but have now lost it and I am starting over and don't remeber much about how I went about doing it then.
The easiest way to download a file from a remote server would be to use copy (http://php.net/manual/en/function.copy.php):
copy('http://someurl.com/image.png', '/var/www/uploads/image.png');
As this function returns a bool, it is easy to determine whether the operation was successful and create a JSON response.
To verify that the file is an actual image, there is unfortunately no way that is 100% sure. It is probably enough to check the mimetype though. You can use finfo for that (http://php.net/manual/en/function.finfo-file.php):
$finfo = finfo_open(FILEINFO_MIME_TYPE);
echo finfo_file($finfo, $filename);
finfo_close($finfo);
For a gif, this would return image/gif for example. You will have to hardcode a list of all mimetypes you want to allow.
I'm making an HTML5/jQuery/PHP app which involves uploading CSV files (via a drag and drop), and processing them to a MySQL database. So far, I can upload the files, and I know how to read them into a database.
My question: I am wondering if it is possible to detect whether a CSV file is in a corrupted format by PHP or Javascript/jQuery? For example, I can rename somefile.png (an image) to somefile.csv, and it still gets uploaded. If I open up the renamed file in Notepad++, all I see is garbage, which is expected.
I would like to do this on the clientside, so I can alert the user (via JQuery) whether the file is in a corrupted format. I'd also like to check on the serverside (via PHP) before I start iterating over each CSV file for db processing.
My first thoughts would be to use regular expressions, but I am unsure how to make ones for this particular problem. I know the basics of regular expressions, but haven't really used them in advanced settings before.
First of all you should check content-type of picked file, it should be "text/csv". At the server-side you can check file via fgetscsv PHP function (http://php.net/manual/function.fgetcsv.php) (catch null or false on error)
You don't want to be validating it if you're going to be reading it right after. Just read it in and catch any errors as you read. That way you come to know whether file is valid or corrupt.
I'm trying to recheck a file on a timed interval. I'm not sure if this can be accomplished, but any help would be greatly appreciated. I understand that as PHP is server-sided, it cannot accomplish my task, so anything with JavaScript is fine.
By rechecking a file, I mean getting a file's contents, but only if it's updated. Somewhat like "if file gets new contents, output them."
If you are checking on a remote file, then the PHP solution would be best: simply send the file and its checksum, then when the checksum changes, the file has been updated.
If you're working with a local file, then (if I'm seeing things right) you'll need a browser with the HTML5 File API. May be a duplicate of this: Check if file has changed using HTML5 File API
I hope the following isn't too tricky.
I have a simple html button. Now I want to open a filechooser as soon as a user clicks on this button.
I do this like the following:
$('.button').click(function()
{
$('<input type="file"/>').attr('value');
});
This opens a filechooser, but I want this file-chooser to only show files on the server, not on the client. I've searched the net but couldn't find an adequate solution so far.
Any proposals are welcome :)
Impossible, sorry. You'd need to use server side code to make a tool that allows the end user to browse the server's files.
The file input is used for the end user to choose file(s) on their machine. It has no knowledge of the server's files.
It's not tricky, but you can't use Input tag for it. The steps are:
Create a module to traverse the directory on your server and output is a JSON format in whatever server implementation that you choose
Create a REST endpoint to give the browser the JSON output from step #1
Use AJAX to call this REST webservice and get the directory listings
Use Tree Widget to basically build the file structure based on JSON (I am sure if you look, one is probably there already for you to use)
There's no simple way to do it. If you use jQuery UI you can use a plugin like this:
http://gusc.lv/jquery/gcmedia.html
With a server-side scripts that outputs a list of the files you want to make browseable.