Php File Upload - About uploading files through php

free web hosting
Free Web Hosting > Computers & Tech > Programming > Scripting > PHP

Php File Upload - About uploading files through php

Supa Comix
Right i have done a check for a tutorial on this as well as a question about it but php is not allowed in the search box. So i thought i'd just ask what i want to know.

I have a form which uploads a file, it refreshes the page, uploads the file and then alerts the user to if the file has uploaded. To be honest im not sure why i keep getting the error. But here is the code:

This is the form that is used for the user to select the file

CODE
<form enctype="multipart/form-data" action="fileupload.php?op=up&user=<?php echo($user); ?>&fid=<?php echo($fid); ?>" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000000" />
Choose a file to upload:
<input name="uploaded" type="file" id="uploaded" />
<input type="submit" value="Upload File" />
</form>


This is the upload code

CODE
if ($op == "up"){
$target = "uploads/";
$target = $target . basename($_FILES['uploaded']['name']);
echo($target);
if (move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo("The file ") . basename ($_FILES['uploadedfile']['name']). "has been uploaded!";
$filesize = $uploaded_size;
$sql = mysql_query("UPDATE tbl_files SET filesize='$filesize' WHERE fileid='$fid'");
}
else {
echo ("Sorry, there was a problem uploading your file.");
}
}


If you could point out any problems with it please do.

 

 

 


Reply

kelvinmaki
Firstly, maybe you could point out what is the error you are getting. From these codes below,

CODE
<form enctype="multipart/form-data" action="fileupload.php?op=up&user=<?php echo($user); ?>&fid=<?php echo($fid); ?>" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000000" />
Choose a file to upload:
<input name="uploaded" type="file" id="uploaded" />
<input type="submit" value="Upload File" />
</form>


It seems to be alright. Of course I assume that the variable $user and $fid is declared somewhere.

Secondly, as for the codes that really does the uploading,

CODE
if ($op == "up"){
$target = "uploads/";
$target = $target . basename($_FILES['uploaded']['name']);
echo($target);
if (move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo("The file ") . basename ($_FILES['uploadedfile']['name']). "has been uploaded!";
$filesize = $uploaded_size;
$sql = mysql_query("UPDATE tbl_files SET filesize='$filesize' WHERE fileid='$fid'");
}
else {
echo ("Sorry, there was a problem uploading your file.");
}
}


If you are going to upload to folder uploads/, then you need to allow permission to write to that folder. Do a chmod for that folder. Did an upload once and found out that its due to permission, it prevent any files from writing to that directory. And of course before all this, you need to mkdir that directory.

There's once I encounter blank page after uploading the files. It didn't show any errors and just blank. This can be caused by trying to upload a huge filesize. If you are running this on localhost, then it should be fine. But if you want to upload a huge file to the server (eg. web hosting), you will need to set the memory limit in the php setup configuration to a higher value. That you can contact the administrator of the web hosting. However, there's another alternative to this approach. You can set the .htaccess file as well

CODE
<FilesMatch "\.(php|html?)$">
    php_value memory_limit 32M
</FilesMatch>


Put the above line to your .htaccess at the root. This method solve for me.

By the way, these are all the problem I encounter for uploading a file. If you encounter the same problem, this might help abit. If not, tell us your error in details. What did you do that got the error and what is the error message. wink.gif

Cheers

 

 

 


Reply

Supa Comix
Awesome. Thanks dude i tried that stuff and it now works! Thanks a lot! smile.gif

Reply

kelvinmaki
You're welcome. Glad it helped.. wink.gif

Reply


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

Recent Queries:-
  1. php protect upload folder - 2.41 hr back. (1)
  2. php file upload file filter - 10.69 hr back. (1)
  3. upload through php post - 131.71 hr back. (1)
  4. php upload via php post - 131.81 hr back. (1)
  5. extjs fileupload.php - 159.17 hr back. (1)
  6. php file upload - 180.83 hr back. (1)
  7. script php file manager upload for cpanel - 196.49 hr back. (1)
  8. extjs fileupload - 253.00 hr back. (2)
Similar Topics

Keywords : php, file, upload, uploading, files, php

  1. How To Include A File Using Absolute Paths Under A Secure Php Installation
    Alternatives to the Include() Function (6)
  2. Magic Quotes And $_files
    (3)
    It recently came upon me that I was designing a website that used file uploads. Of course, PHP was
    my first choice to solve the problem. I'm pro-PHP and anti-ASP. Perl is somewhere in the middle.
    But anyway! I have magic_quotes_runtime and magic_quotes_gpc both on and it seems to be causing
    some problems with file uploads. If a user uploads a file called "Jared's Stuff.txt" (that was
    one of my tests) then magic_quotes insert a backslash before the apostrophe and $_FILES becomes
    "'s Stuff.txt" since the backslash is interpreted as part of the path. Is the....
  3. How To Protect Included Files
    (10)
    Hey! How can i make my included files 100% safe. Like if I include a file witht his code.. CODE
    include "bla.php"; How can i make it 100% safe? I know I must close the php tags in the
    included files. but what more =?....
  4. Reading Files And Folders
    (1)
    I am in the process of writing a small content management system for my niche sites and need to be
    able to accomplish the following two items. Any assistance would be appreciated: I have a
    directory that has nothing but sub directories in it and I need to be able to identify each
    directory name and return them in an option drop down selection menu which will be included in an
    identifying URL. ALSO: I am in need of a script that could be included on a returned page
    that will read the content of a table and identify various keywords within the content and pl....
  5. Extplorer
    A PHP -and JavaScript- based File Manager (7)
    Browsing the ExtJS examples website i found this excellent web-based file manager called
    eXtplorer . eXtplorer allows you to browse your webserver folders with an intuitive Layout which
    makes working with files very easy, and thanks to the great ExtJS Javascript Library you can drag
    & drop folders and files, filter directories and sort the file list using various criteria. You can
    use eXtplorer to for example: browse directories & files on the server. edit, copy, move, delete
    files. search, upload and download files. create and extract archives. create new fil....
  6. Php Uploading Problem
    (6)
    I thought there's no trouble with it. But it warned CODE Warning: copy(E:\yufeng\upload1)
    : failed to open stream: Permission denied in E:\yufeng\upload.php on line 16 What's wrong?
    CODE uploading files uploading files:
    if(isset($submit)){ if($myfile!= "none"){ $upload_dir = "E:\yufeng\upload1";
    $up=copy($myfile,$upload_dir); if($up==1){ print("Files were uploaded successfully!"); } else {
    print("file uploading failed!"); } unlink($myfile); } else{ print("You haven't select any
    files!"); }....
  7. Help To Transfer Files Within Hosting Space Using Php
    (4)
    I want to transfer files from one folder to another or an entire folder in my web space. I don't
    want to download it and upload it, can I do this using a PHP script. Please help me. I'm just
    learning the basics of PHP....
  8. Automated File Structure Creation Script
    As Requested By Mark420 (3)
    While chatting with Mark420 today on the shoutbox, he mentioned that he was looking for a script to
    create his entire folder structure with just a click of a button. For example, he wanted the
    following folders created in his root folder by just clicking submit. /images /images/thumbs
    /images/icons /css /javascripts /content /content/articles /content/tutorials Presumably this
    would be used for some type of installation system or other quick server setup situation. Anyhow
    here is what I threw together for him: /* ********************************************....
  9. How To Force A Zip File To Be Downloaded
    (11)
    Hi, i have a problem with a zip file that i want to be force downloaded by any client, i know that
    with the header function it can be achieved but dont know why it doesnt works. I have two files, the
    first one is a simple html file with an a tag and the second is a php file, here is my code: html
    file: CODE Download File Php file: CODE header('Content-Type:
    application/zip'); header('Content-Disposition: attachment; filename="file.zip"');
    readfile('downloads/file.zip'); ?> May be i forgot something, does it is necessary to ....
  10. How To Delete File Using PHP Shell Script
    (3)
    i have this problem regarding file access seems that my admin host or the server system itself have
    locked up the acces to create new file, delete a file.. change file permissions and such controls
    to file.. someone told me to use php shell script.. can you help me out here? ******** i just
    need to find out how to use php shell script and add it to my php scripts to delete a file.. thanks....
  11. File Self Secure?
    is it avaible (6)
    I just learn php. We store the pass word of Mysql in a file right. So is there any way to may a pass
    protect that file . i mean they could hack and find out the place of the file (ex like in forum) and
    drop all sercure data /huh.gif" style="vertical-align:middle" emoid=":huh:" border="0"
    alt="huh.gif" />....
  12. How To Edit Php Files?
    (16)
    Hi, You can use frontpage to preview HTML files, but how can I edit and preview PHP files offline.
    I'm not well-known with PHP and I see more and more using this script to manage their site.....
  13. Updating An Rss File Using A Php Form
    (1)
    Hi, I'm currently making a site for my organization but I'm stuck on this one section where
    I would have to make a PHP form in order for other members to post an item into my RSS file. So does
    anyone know a PHP script I can use in order for me and other members to input a new RSS item into my
    news RSS file?....
  14. Xgrid With Php
    Creating a script to post a blender file to Xgrid using PHP (0)
    I am doing pre planning for the blenderxgrid.com script. I was originally going to do this with
    PERL, but elected against it. Eventually I'll be moving the site off astahost and replacing it
    with a website hosted on the same machine as the xgrid controller. I am setting up a test version
    on my latop using OSX's apache server, MySQL, and PHP on a localhost config. Here is my step
    list for the script: ------------------------------------- Form: (within Xoops CMS, so user will
    have to be logged in) Username Password (where they can upload the .blend file....
  15. How To Delete Files When Session Ends
    (4)
    Dear Friends I need solution to a problem. The problem is as under: I am creating certain files
    (playlist) in server disk when user selects some songs. The files are created in ram format. What I
    want to do is to delete these files created during a particular session. Is it possible to do so?
    Now I am deleting these files using on Unload event fired by JavaScript. I am using PHP. ....
  16. How To Embed Ram File Produced By Http Header
    (2)
    Dear Friends I want to stream music from my website. The file format is .rm. People say that one
    need a Helix Server or other media server to stream. I have found a solution to this problem as
    well. I read an article about streaming music. It tells that if the size of the media file is small
    and the byte rate of the media file is lower than that of the user internet connections byte rate,
    the file get streamed automatically from HTTP server. In theory, any file is "streamed" by a web
    server that is, sent back to the client in small pieces. What makes media files speci....
  17. Deleting Files With PHP
    is this possible? (6)
    It is posible for PHP to delete files on a server? If so, how is this possible? Just out of
    interest.......
  18. Generating A Table Into A File In CSV Format
    and letting user download the file (6)
    I'm working on a project, part of which consists of working with large tables of different
    kinds. Now, I'm using a page seeking technique which allows you to browse through the records
    depending on which page you want to see and how many records you want to see on each page. Now, I
    need a link (or a form button) on each page which, when clicked, will throw a file to the user for
    download (the download window should popup immediatly) which will give the part of the table,
    currently being viewed, in CSV format. One way I can think of to generate the file is manu....
  19. PHP Script To Upload A File
    with password-protection. (13)
    Problem: Upload a file to the AstaHost account (that I have been granted here) throught the
    webpage I would like to know, how should I proceed on this particular problem. I know this has
    been done through cPanel, but I want to write a PHP functionality. The cPanel is accessed through
    the 2082 port, and most of the places I access internet from does not give me access through that
    port. I can access http://www.kmaheshbhat.astahost.com/ but not
    http://www.kmaheshbhat.astahost.com/cpanel or http://www.kmaheshbhat.astahost.com:2082/ . I need
    to go to one particu....
  20. Display Text If Line Not Empty In Config File
    (8)
    I have been working on a new template and I would like it so that if in the global configuration
    file, I have a variable for a global site announcement that would go on every page. The line in the
    global configuration file is this: CODE $announcement = "ANNOUNCEMENT"; In my template file,
    I could easily add CODE but that would leave a blank space and with the announcement style
    (similar to the Invision Power Board error box). Is there some sort of script that could be put in
    the template page so I could have the global config file look something like t....
  21. PHP File Upload Works... But Stupid IE
    (3)
    ok i have used the following code in my upload.php file Code: CODE $uploaddir =
    '../photos/'; $uploadfile = $uploaddir . basename($_FILES ); echo ' '; if
    (move_uploaded_file($_FILES , $uploadfile)) {   echo "File is valid, and was successfully
    uploaded.\n"; } else {   echo "Possible file upload attack!\n"; } using ../photos/ as my
    upload DIR works, as the file does upload, but when i echo $uploadfile on the same page: Code:
    CODE " alt="uploadedfile" /> IE wont show it as it has two fullstops before the photo dir
    in the domain....
  22. Uploading Image Via Admin Menu?
    (2)
    There's never much action in here. Here's a question for all you php guys out there.
    Here's what I want. Let's say I create a simple administration menu for someone to
    Add/Edit/Delete a record/item from a database. Now in the add function, let's say I want to
    store a URL to a image. Rather than having to go in and FTP that image up on the server, how could
    a person create a 'Browse' button and find that image on your harddrive, then create a
    script to upload the image to the server? Just wondering how to do it, or if it could be done e....
  23. Looking For Upload Scripts That Support Over 150MB
    help (3)
    ive heard upload scripts have a upload limit of 2MB, is this true and does anybody have a upload
    script that supports 150mb ... plz let me know thanks bash18....
  24. Php : Variables Included Dont Work In Functions
    Variables from Included files dont work (4)
    Today, I came up with this strange PHP behaviour. Just wanted to know if anyone has any
    suggestions! I make a common variable/function file called config.php. I put in my generally used
    functions in it. Suppose this is my file // -----VARIABLES --- // $a=10,$b.... //
    -----FUCTIONS--- // function doit() { print "A value is " . $a; } ?> Here, suppose we execute
    this file directly. Since A has a global scope, it does work perfectly. But if this same file is
    imported in another file say, mainfile.php // -----VARIABLES --- // $c,$d.... include
    'config.ph....
  25. Online HTML/PHP Editor: Edit File In Browser!
    (8)
    Im wondering how you can load a file into a text area and edit it. Lets say i have a php file called
    test.php and id like this file to be loaded into a textarea and the user should be able to edit this
    file and store it. Is this posible? Im also wondering how you make it so the html dosnt get
    encoded by the browser, lets say i need to explain on my website how you use html. CODE Just
    testing something, maybe i can get this from the source. ....
  26. How Do I Create And Write To Files?
    creating, writing, deleting files (4)
    Hi, Can someone please tell me how to create files and write to them in PHP. I just want to create
    a simple file containing text, and then be able to read it or update it. Thanks Alfie....
  27. Counter With Img In Flat File
    (2)
    this is a counter with images and stor in flat file becouse i can not upload .zip .rar file iwell
    program it on this post at frist you need to 2 files count.php count.txt and you need else make
    folder has name gifs and make 10 pictuer 10 file 0.gif to 9.gif now all ok open the count.php and
    add this code CODE ### IMAGE FORMAT $format = ".gif"; $file = file("count.txt"); $num =
    ($file + 1); exec("echo $num > count.txt"); switch($type) { case "text":  echo $num;  break;
    case "gfx":  $i = 0;  $cntn = strlen($num);  while($i   $tmpnum = subst....
  28. Php Reading And Writing To File
    the code is very esay (4)
    this code to Read from file you can use this code in to make a small data base and it is very to
    use CODE $fp = fopen ("file.txt", "r"); $bytes = 4; $buffer = fread($fp, $bytes); fclose
    ($fp); print $buffer; ------------------- to write to same file CODE $fp = fopen
    ("file.txt", "w+"); fwrite ($fp, "Test"); fclose ($fp); ---------------- thanks and iwait
    the commant....
  29. Getting Files From Other Sites
    Using wget (3)
    Hello. A while ago, a friend showed me this PHP code (wget) which allows you to get files from other
    sites. I was a simple one liner type thing. Since my old host (Nytka) went down, I've lost it,
    as has my friend. Do you know anything about wget, and could tell me it?....
  30. Php Script To Download File From Another Site
    (9)
    hi i need a php or java script code for downloading files from other sites to my site for example:
    http://download.com/file.zip to http://mysite.com/file.zip thanks....

    1. Looking for php, file, upload, uploading, files, php






*SIMILAR VIDEOS*
Searching Video's for php, file, upload, uploading, files, php
advertisement




Php File Upload - About uploading files through php