spacepoet Posted January 28, 2011 Share Posted January 28, 2011 Hello again: I would like to see if someone can either show me some code, or point me in the direction of a good article about uploading a file (meaning a photo, .JPG, .GIF, etc) via PHP. I have only done this with ASP. My searches on GOOGLE are only showing me ways to upload a file INTO the database, and I don't believe that is the proper way to do it. I have always stored the file (.JPG) in a folder called "uploads" and stored the file name (myPhoto.JPG) in the database. So, if I want to make a Team bio list with a photo, a title, and a description what is the proper way to do that? Meaning, I click "Add Player" and a form with title, description, and a photo upload field appear. The data is filled out, "Submit" is clicked, and the photo is uploaded to "uploads" and all other data is inserted into the database. I had wanted to be able to modify, delete, or make inactive each player, and use a dropdown SELECT menu to choose what player to edit. Any ideas how to accomplish this? I would appreciate the help! Quote Link to comment https://forums.phpfreaks.com/topic/225907-help-understanding-how-to-do-a-file-upload-team-bio-list/ Share on other sites More sharing options...
QuickOldCar Posted January 28, 2011 Share Posted January 28, 2011 Gonna break this down for you. make a form form hits an upload script the file type is checked,max size, name sanitized, and unique timestamp also added to the name of the file. You can make a folder lets say images that will be the path to the image you append the image path to the filename, path/filename save these results into a field in a database you place your uploaded file into that folder You could also add additional information to the database for each mage, like who uploaded it, the current date, what type it is, it's size,etc... If all went well you now have a database that can call to that knows where the exact location is for image. Call to mysql with select statements in whatever way you wish to display them Quote Link to comment https://forums.phpfreaks.com/topic/225907-help-understanding-how-to-do-a-file-upload-team-bio-list/#findComment-1166348 Share on other sites More sharing options...
spacepoet Posted January 28, 2011 Author Share Posted January 28, 2011 Hi: Thanks for the reply. Do you have an example of this, or can you point me to one online? I do like the appending Time/DateStamp to images so one never overwrites a file of the same name. I have done all of this with ASP programming, but I am trying to learn how to do it with PHP haven't found an example of this format online. I keep finding ones the insert the file into the database. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/225907-help-understanding-how-to-do-a-file-upload-team-bio-list/#findComment-1166359 Share on other sites More sharing options...
jcbones Posted January 28, 2011 Share Posted January 28, 2011 Simple file upload tutorial Of course, you need to incorporate everything above into that script. Quote Link to comment https://forums.phpfreaks.com/topic/225907-help-understanding-how-to-do-a-file-upload-team-bio-list/#findComment-1166369 Share on other sites More sharing options...
QuickOldCar Posted January 28, 2011 Share Posted January 28, 2011 Yeah I tried out the above tutorial, one of the few that actually works, but needs quite a few changes. I made a function to add a timestamp to a file name whether it be in a folder or the single file. <?php function addTimestamp($filename) { $character = "/"; $target_check = strrchr($filename,$character); if ($target_check[$character]) { $temp_filename1 = end(explode($target_check[$character],$filename)); $target = true; } else { $temp_filename1 = $filename; } $temp_filename = strtolower(trim($temp_filename1)); $timestamp = date('Y-m-d-H-i-s');//this one more readable //$timestamp = time();//or uncheck this and use time if ($target) { $new_filename = str_replace($temp_filename1, "$timestamp-$temp_filename", $filename); } else { $new_filename = "$timestamp-$temp_filename"; } return $new_filename; } ?> <?php //some example files $file1 = "images/SomeCoolPic.png"; $file2 = "http://mysite.com/path/my-cat.jpg"; $file3 = "pictures-of-flowers.jpg"; $file4 = "thumbnails.png"; $file5 = "PEOPLE.png"; //this will just add the timestamp wherever you call for it $file1 = addTimestamp($file1); $file2 = addTimestamp($file2); $file3 = addTimestamp($file3); $file4 = addTimestamp($file4); $file5 = addTimestamp($file5); //gonna echo to see the new file names echo $file1,"<br />"; echo $file2,"<br />"; echo $file3,"<br />"; echo $file4,"<br />"; echo $file5,"<br />"; ?> The results look similar to this images/2011-01-28-02-14-22-somecoolpic.png http://mysite.com/path/2011-01-28-02-14-22-my-cat.jpg 2011-01-28-02-14-22-pictures-of-flowers.jpg 2011-01-28-02-14-22-thumbnails.png 2011-01-28-02-14-22-people.png Quote Link to comment https://forums.phpfreaks.com/topic/225907-help-understanding-how-to-do-a-file-upload-team-bio-list/#findComment-1166371 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.