tanknd
08-25-2002, 08:16 AM
Does anyone know a quick and easy way to upload files to my server via PHP? I am creating a PHP backend for my site and I want to be able to upload files directly from there if that helps. Thanks a lot.
![]() | View Full Version : Upload Files tanknd 08-25-2002, 08:16 AM Does anyone know a quick and easy way to upload files to my server via PHP? I am creating a PHP backend for my site and I want to be able to upload files directly from there if that helps. Thanks a lot. Mark-TFL 08-25-2002, 08:34 AM Assume that the file field in your form is called "file", make sure in your <form> tag you include: enctype="multipart/form-data" Then in your PHP script: $target = "./upload_dir/" . $file_name; if(copy($file,$target)) { echo("File Copied Successfully."); } else { echo("File could not be copied."); } If you want to include checking of the size of the file, stored in the variable: $file_size will be the size of the file (in bytes) so you can do error checking that way. Moreover, if necessary you can check the MIME type of the file which is stored in the variable $file_type. This is because when you upload a file, PHP will automatically created: $file_size $file_name $file_type Which are pretty self explanatory. $file is the link to the temporary file which in the code you copy to the desired location. Hope this helps. AlCapone_yg 08-25-2002, 08:39 AM Little note - it is adviced to use move_uploaded_file() instead of copy(), as there are some security issues. Although if you're using old version of php, you do not have any choice. tanknd 08-25-2002, 09:54 AM Thanks. Couple Questions That 'upload_dir' in the $target definition... do I need the absolute path there? I am not positive the path that it is at on the server I am hosting with. Is there a variable that lets me use relative paths instead of absolute so that I can move this between systems? Thanks again. combs 08-25-2002, 10:26 AM You can use absolute path but as you don't know the folder, you can use Server map path variables to get the HTTP_REFERER for knowing the current path or the MAP_PATH info. tanknd 08-25-2002, 10:35 AM I just figured out what was happening. Stupif mistake on my part. Thanks a lot for the help! works like a charm as soon as I set the permissions correctly on the folder. Thanks again Curtis H. 08-25-2002, 11:52 AM Sounds like you have everything under control but for those who may not want to write their own, here's a couple "uploaders" that work nicely. Uploaders (http://www.zachwhite.com/scripts.htm) dreamrae.com 08-26-2002, 01:22 AM remember php and apache server limits the max file size on a http or https file transfer.. |