www.webdeveloper.com
Results 1 to 5 of 5

Thread: PHP upload file (pics, in zipped format, rar file).

  1. #1
    Join Date
    Jul 2012
    Posts
    23

    Lightbulb PHP upload file (pics, in zipped format, rar file).

    Hi,
    I have some data to upload through php and the data could be in zipped or RAR format and file size could be more than 400MB. So i need some good code to work on this with minimum data loose and error.
    Could it be possible to upload such a large amount of data through php, if yes then i request to you please suggest some good stuff to use.

    Thanks

  2. #2
    Join Date
    Feb 2014
    Location
    Canada
    Posts
    155
    You can upload large amounts of data, however, you need to configure the web server to allow it. If you're using Apache, you'll have to configure your php.ini file by going to the File Uploads section and change upload_max_filesize to the limit you want (it's a very good idea to have a limit). If you're not using Apache, you'll still have to configure it because the server will have a default which will definitely be well under 400 MB. After that, you should verify the uploaded file. I'm not sure how much you want so I'll give just a short bit of code from something I have lying around (note: for this I used a max file size of 200 MB but just change that to whatever max you want. It only accepts file size in terms of bytes, so use Google to convert your max to bytes). The below doesn't check whether the file already exists, I'm not sure what you entirely wanted so I didn't include it.

    With extremely large files, it's possible the upload may time out, in which case you may need to do more configuration to the php.ini file as well as .htaccess.

    The below code should be executed after the used has uploaded a file from a form.

    PHP Code:
    <?php
    #array of case-sensitive file types that are not permitted
        
    $invalidTypes = array('air''app''bat''cgi''cmd''com''esh''exe''gadget''hta''jar''js''jse''jsx''pl'
        
    'py''pyc''pyo''reg''rgs''scr''script''sct''udf''vb''vbe''vbs''vbscript''ws''wsf');
        
        
    #checks if there is any error before uploading a file
            
    if($_FILES['file']['error'] != 0) {
                die(
    "Failure to upload due to error ".$_FILES['file']['error']);
            }
        
        
    #checks if the user chose a file to upload before clicking "upload"
            
    if(!filesize($_FILES['file']['tmp_name'])) {
                die(
    "Please upload a file");
            } 
        
        
    #checks if the user's file is less than 200 MB /* change this part to whatever max file limit you want (highly recommended to have a limit!) 
            
    if($_FILES['file']['size'] > 209715200) {
                die(
    "File currently is ".$_FILES['file']['size']." but must be less than 200 MB");
            } 
        */
        
    #updates file permissions
        
    chmod($_FILES['file']['tmp_name'], 777);
        
        
    $fileInfo pathinfo($_FILES['file']['name']);
        
    $fileExt $fileInfo["extension"];
        
        
    #checks whether the file is of a valid extension
        
    if(in_array($fileExt$invalidTypes)) {
            die(
    "The file type ".$fileExt." is not permitted");
        }
        
        
    #retrieves the most recent date of file modification
        
    $fileDate date("d-m-Y, H:i:s"filemtime($_FILES['file']['tmp_name']));
        
        if(
    move_uploaded_file($_FILES['file']['tmp_name'], $targetPath)) { # targetPath is the path where it will be uploaded
            
    echo("Success!");
        } else {
            die(
    "Fail!");
        }
    ?>

  3. #3
    Join Date
    Jul 2012
    Posts
    23
    Hi,
    Thanks For your reply.
    This a very helpful code for me. I only know how to change the limit in php.ini file but i am not aware of the .htaccess file. I will be very thankful to you, if you can suggest some for that too.
    as i mention the file would be zipped or rar file may be 400MB of above but less than 500MB.
    Thanks

  4. #4
    Join Date
    Jul 2012
    Posts
    23
    Hi,
    I have posted same in the other forum too, a guy reply something like this.
    """""
    PHP can upload as much file size as it can I believe. Your huge problem you will encounter on large file size is your php.ini or .htaccess. I have created my own "YouTube" sort of file uploading where you can only upload videos, but the file size are too large so I get the 413 error.

    I'm not sure if it'll help you, but you need to change the max_upload_size and max_upload_limit, but I've tried this and it still doesn't work. Tried it on .htaccess and it gives me error 500 apache misconfiguration.

    It's not working for me so I stopped building that application for a while now, but I'm certain that someone has gotten it to work. Your issue will probably be php.ini or .htaccess if you have access to either both of them.
    """""
    What could be the cause of these problem? would these will come while using the code you have given to me?
    suggest pls

    Thanks

  5. #5
    Join Date
    Feb 2014
    Location
    Canada
    Posts
    155
    The first thing is to make sure that Apache was properly working and configured before you edited the php.ini file. With the code I provided, you need to have another page (can be purely HTML) that just takes in a file. If you don't have that then the code will not work. You should have something like the following, where verifyFile.php is the code I provided earlier. You also have to ensure you have a folder called "uploads" or whatever other name of your choosing in your htdocs folder as that is where the file will be uploaded to. The error you're getting is due to misconfiguring Apache and not a result of the code I provided below or in my previous post.

    If you cannot resolve your error by undoing whatever changes you did to your php.ini file, uninstall it and download the XAMPP installer (automatically does all the configuration, you just should create a back-up php.ini and modify the max_upload_size properties).

    HTML Code:
    <form enctype = "multipart/form-data" action = "verifyFile.php" method = "post">
    			<table border = "0" cellpadding = "2" cellspacing = "5">
    				<tr>
    					<td>
    						<label for = "fileName">Select file to upload</label>
    					</td>
    					<td>
    						<input type = "file" name = "file" id = "fileName" placeholder = "Choose file path" autofocus = "autofocus" required = "required" />
    					</td>
    				</tr>
    				<tr>
    					<td></td>
    					<td><input type = "submit" value = "Load file" /></td>
    				</tr>
    			</table>
    		</form>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles