my requirement is this :
" When users uploaded one file say "sample.tex" then i need to find the same name PDF file in that directory once he upload the "sample.tex". so file name should be "sample.pdf". we have one form that contain two input file fields.. check the image for reference.
http://img40.imageshack.us/i/proofbb.png/
once user upload the first file and click the "Show Author Email(s)" then i need to find another file "sample.pdf" in the same path and put in below file field. Is that possible in PHP or JQUERY or watever... not only PHP Even Java is also fine. Please help me to find the solution.
Regards
Dipen
You cannot do that with plain JavaScript. You can't even obtain the path information from your "first file"; all the browser will tell you is the plain file name (that is, the file name without any path information). You also cannot force a "file" input field to be set to a value.
You might be able to do this by creating a signed Java applet, but that's a whole different enchilada and you'd pretty much have to make the whole form be a Java thing.
(There's nothing you can do from PHP, as all the server will get is the plain filename and no path information at all.)
Let the user do it. Use uploadify with multiple simultaneous uploads enabled. So simple.
Related
I don't want to read file using tmp_name. Is there any other possible methods to read file from a folder with its original name that I stored in databases?
Also, I am trying to access contents of that file in second form.
All else being equal: No.
A file was uploaded and it was saved to a temporary place with a generated filename and your PHP program was told what filename the client said it should have. The two filenames are not the same.
You can't access it using its original file name because that isn't the file name it has on the computer running the PHP.
The script handling the upload could use move_uploaded_file to move the file to somewhere else and change its name so it is the same as the one the client told you it originally had. Then you would be able to use that name. You would, of course, have to be careful to avoid collisions. Two different files with the same name could be uploaded.
I have used this step echo $_FILES["fileField"]["tmp_name"]; but result like this.
C:\xampp\tmp\phpA9EE.tmp
How can i get exact file path?
An uploaded file does not have a "full path", other than temporary location where PHP has stored it during the upload process.
For the security of users, the browser sends only a filename of where it came from on the remote computer; for your security, you should not blindly use this (security rule of thumb: anything sent by the user is suspect and could be used to attack your system). You might want to filter it through a whitelist (e.g. remove anything other than letters and numbers) and use it as a "friendly" upload name, or you might want to ignore it completely. The browser also sends a file type (e.g. image/jpeg); again, this should not be trusted - the only way to know the type of a file is to use a command that looks at the content and validates it.
As far as PHP is concerned, what has been uploaded is a chunk of binary data; it saves this to a randomly named file, which is the path you have echoed there. The PHP manual has an introduction to how this works.
With that path you can do one of two things:
validate with is_uploaded_file(), and read the data with file_get_contents() or similar
use move_uploaded_file() to put it in a permanent location of your choice
I have a PHP file that has a $_FILES variable in it, and I obviously have a temp file of the file uploaded saved. Problem is that what I want to do is let the user upload the image, validate that it's okay (both via 1st php file), and then allow the user to enter info about the image. From the info I can obtain the name of the image and use move_uploaded_file() to save the image (on the second PHP). The problem is that the file uploaded to the temp file is, well, temporary, and so I can't use it in my second PHP file. Is there any way to go around that? I can move_uploaded_file() in the first file but I'm looking for something easier. I still want it to be like a temp folder in the sense that the file is temporary, but I want to keep it for a couple of minutes after the execution of the first PHP file...
Thanks.
The first page - the one that accepts the file - will need to use move_uploaded_file(). You cannot escape this requirement.
However, you can use tempnam() to create a new temporary filename and use move_uploaded_file() to copy the file to that name. Then pass that second filename to the second page so it knows where it is.
The other alternative is to collapse all the input and processing into one POST, so that all the information is entered at the same time the file is uploaded. This approach has always worked for me.
Ones again I have a question which I hope to get help with.
I have a small database where I want to include information about the location of specific pdf files. This means that I am not saving the actual pdf’s just their location on a local drive.
The issue is that I don’t need to do any upload of the file since they are located where I want them to be beforehand.
My actual form code is a type="file" code with a browser button. It saves a copy of the pdfs into pdffiles map. What I want is to keep the browser button functionality but without the need of saving the files in the pdffiles map, plus that I want the saved pathway in Mysql code to be the original location of the file.
I hope you can understand what I am trying to achieve.
Thanks to you all!
Unfortunately, my knowledge in JavaScript is limited.
The form line I am trying to read is simple:
Say I have a file: C:\test\Ritningsregister\pdffile1.pdf
Like I said before all I want is to store the path info of the current file into MySql without any upload/download involved. I have been trying to use pathinfo($_FILES['filename']['name'], PATHINFO_DIRNAME) but it only gives me the relative path, ie .
pathinfo($_FILES['filename']['name'], PATHINFO_BASENAME) returns pdffile1.pdf as it should.
I have also trying to use realpath($_FILES['filename']['name']) hoping it will give me the whole path information: C:\test\Ritningsregister\ … without any luck.
What I at least could establish is that when I am trying to open a pdf file whose pathway has beforehand been imputed into MySQL say: C:/test/Ritningsregister/pdffile1.pdf. The browser had no problem in opening it.
So I really hope you can help me to solve this problem!
Best regards CaLey
[edit]
well what you could do then is use javascript to grab the value from the input-file and put that text into the value of a hidden input, and then reset the input-file so no file is uploaded. if you need help working out how to do this let me know :)
[edit]
are you just trying to process some file locations into a database ? as you can use php scripting to just read a directory's file's and then save that into a db without a form at all
I am in the middle of making a script to upload files via php. What I would like to know, is how to display the files already uploaded, and when clicking on them open them for download. Should I store the names and path in a database, or just list the conents of a directory with php?
Check out handling file uploads in PHP. A few points:
Ideally you want to allow the user to upload multiple files at the same time. Just create extra file inputs dynamically with Javascript for this;
When you get an upload, make sure you check that it is an upload with is_uploaded_file;
Use move_uploaded_file() to copy the file to wherever you're going to store it;
Don't rely on what the client tells you the MIME type is;
Sending them back to the client can be done trivially with a PHP script but you need to know the right MIME type;
Try and verify that what you get is what you expect (eg if it is a PDF file use a library to verify that it is), particularly if you use the file for anything or send it to anyone else; and
I would recommend you store the file name of the file from the client's computer and display that to them regardless of what you store it as. The user is just more likely to recognise this than anything else.
Storing paths in the database might be okay, depending on your specific application, but consider storing the filenames in the database and construct your paths to those files in PHP in a single place. That way, if you end up moving all uploaded files later, there is only one place in your code you need to change path generation, and you can avoid doing a large amount of data transformation on your "path" field in the database.
For example, for the file 1234.txt, you might store it in:
/your_web_directory/uploaded_files/1/2/3/1234.txt
You can use a configuration file or if you prefer, a global somewhere to define the path where your uploads are stored (/your web directory/uploaded files/) and then split characters from the filename (in the database) to figure out which subdirectory the file actually resides in.
As for displaying your files, you can simply load your list of files from the database and use a path-generating function to get download paths for each one based on their filenames. If you want to paginate the list of files, try using something like START 0, LIMIT 50; in mySQL. Just pass in a new start number with each successive page of upload results.
maybe you should use files, in this sense:
myfile.txt
My Uploaded File||my_upload_dir/my_uploaded_file.pdf
Other Uploaded File||my_upload_dir/other_uploaded.html
and go through them like this:
<?php
$file = "myfile.txt";
$lines = file($file);
$files = array();
for($i=0;$i<=count($lines)-1;$i++) {
$parts = explode($lines[$i]);
$name = parts[0];
$filename = parts[1];
$files[$i][0] = $name;
$files[$i][1] = $filename;
}
print_r($files);
?>
hope this helps. :)
What I always did (past tense, I haven't written an upload script for ages) is, I'd link up an upload script (any upload script) to a simple database.
This offers some advantages;
You do not offer your users direct insight to your file system (what if there is a leak in your 'browse'-script and you expose your whole harddrive?
You can store extra information and meta-data in an easy and efficient way
You can actually query for files / meta-data instead of just looping through all the files
You can enable a 'safe-delete', where you delete the row, but keep the file (for example)
You can enable logging way more easily
Showing files in pages is easier
You can 'mask' files. Using a database enables you to store a 'masked' filename, and a 'real' filename.
Obviously, there are some disadvantages as well;
It is a little harder to migrate, since your file system and database have to be in sync
If an operation fails (on one of both ends) you have either a 'corrupt' database or file system
As mentioned before (but we can not mention enough, I'm afraid); _Keep your uploading safe!_
The MIME type / extension issue is one that is going on for ages.. I think most of the web is solid nowadays, but there used to be a time when developers would check either MIME type or extension, but never both (why bother?). This resulted in websites being very, very leaky.
If not written properly, upload scripts are big hole in your security. A great example of that is a website I 'hacked' a while back (on their request, of course). They supported the upload of images to a photoalbum, but they only checked on file extension. So I uploaded a GIF, with a directory scanner inside. This allowed me to scan through their whole system (since it wasn't a dedicated server; I could see a little more then that).
Hope I helped ;)