I know this should be really simple, but im having trouble getting this to work right, basically i want to have an image upload box that uploads an image and then puts the new url into a mysql database. Anyone have any advice on how to do this, as i may be having developer block but im over complicating it in my head :P Thanks
Uploading files using php.
Assuming some kind of LAMP server, the uploaded files will be placed in a temporary directory. You will have to move them from there to their final destination and I usually give the moved files a new file name to avoid any security problems. Of course the original file-name is available so you can keep it as well, either as a file-name or as an entry in a database.
What are you precisely having problems with?
Related
I am building a website where I am uploading images to my ftp folder through PHP script. Now I want to display those images on to my HTML pages. I was thinking about using PHP and getting array of all the images from my ftp folder and then display them using image view.
Please tell me if I am doing this the wrong way and if there is any other better alternatives to it. I was reading php manual for ftp_nlist and ftp_rawlist but did not understand.
Well it may depend on how many images you have in there. Probably the most "correct" way to do it would be to store the filenames in a DB. You could scan the entire folder, but for every single request that's potentially a lot of overhead rather than just grabbing them out of a DB.
Are you manually uploading the images? Give us more details on how that works and we can better serve you. If you're using a script to upload images (I've had lots of projects where that's the case), then you can just have the script insert those filepaths into the DB for you. If not, (you're manually uploading them), or if indeed there are not a large number of files, then scanning the folder wouldn't necessarily be a bad thing. I've used that method on smaller projects myself.
Read up on the php readdir function in the docs (which actually works a lot like mysql_fetch_assoc, ironically)- That will provide you with an excellent way to go without setting up a DB. For an approach where an upload script handles it, I recommend a DB. Without more info, it's hard to say.
Good luck!
I have a quick question if anyone could help. I am building a CMS for a client where they can log in, and change content (including pictures via upload file form) that are all stored in a database.
My question.. I have been researching, and everywhere says I need to store the image files outside the root folder. Is this necessary in my case if only a few people will be uploading files, inside an admin panel, where they must first log in to the site? I will have already taken steps client side by making sure of file type, size, extension etc... then changing the name of the file before adding it to my DB... Is this secure enough, or am I asking for trouble down the road?
Thanks
Its generally a good idea to store uploaded content someplace where it cant directly be addressed by a browser. You dont want someone uploading a .php file (or some other format you forgot to check for) and then being able execute it by pulling up the direct url. Rather, you'd have a wrapper script that delivered the file.
So yes, its a good idea, but not 'necessary' (by the dictionary definition of the word). You can certainly choose not to do so if in your judgement the admin area is otherwise secure.
That said, in the scenario you describe, as long as its only admin users who can upload images, I dont think its a huge deal either way.
btw, if you are not already, verify the images by their file headers or content, not file extension.
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
A friend of mine told me that saving images in the database is not much of a good idea. Instead save it to the directory and then save it's name in the database and when I want to display the images in the browser I'm just going to use the name in the database to find the picture. Can anyone please guide me on how to do this starting from scratch?
Any help would be much appreciated.
By the way I am using php and MySQL.
Do two things:
accept more answers, here's how: https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work
read this tutorial: http://articles.sitepoint.com/article/php-gallery-system-minutes
What is it exactly that you don't know how to do? Your question seems to contain its own answer:
set up a directory for images, accessible to your web server
copy some image files in there
go to phpmyadmin and create a table with a filename field, for example of type varchar 100
store the image filenames in the table
write a php script that fetches image names and displays the HTML to load the images
I need a php code and sql code that will let someone upload an image to a database. The only thing I can find is very glictchy and not accepted by some browsers. Any ideas?
NEVER store images in the database. NEVER EVER EVER EVER. There are tons of other questions posted here about it that you may want to ready up on.
Always store directly on filesystem, and store the image URL of the file in the database.
If I understood correctly, you want to upload image files (or any files) via browser to the server and save them in the database. If that is the case, read this:
http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/uploading-files-to-mysql-database.aspx
There are likely lots of available tutorials online to show you how to do this (you might take a look at this one: http://www.codewalkers.com/c/a/Database-Articles/Storing-Images-in-Database/).
I think that this is not the most efficient way to handle images, however. You might consider writing them to a folder and simply keep the name of the file and its location in the database. This stackoverflow question might help: How to store file name in database, with other info while uploading image to server using PHP?