Note that there are some explanatory texts on larger screens.

plurals
  1. POUpload files with ajax? (PHP)
    primarykey
    data
    text
    <p>So I'm making a small script for myself, where I can upload files to my server. It works pretty well now, but whenever I try to upload bigger files it just crashes. So I think what if I'll add ajax support to make it: 1. Work better; 2. Look better.</p> <p>So, this is what I have so far:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;File Uploader v1.0&lt;/title&gt; &lt;link rel='stylesheet' href='style.css'&gt; &lt;a href="index.php"&gt;&lt;img src="img/logo.png"&gt;&lt;/a&gt; &lt;/head&gt; &lt;body&gt; &lt;center&gt; &lt;table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"&gt; &lt;tr&gt; &lt;form action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;" method="POST" enctype="multipart/form-data"&gt; &lt;td&gt; &lt;table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"&gt; &lt;tr&gt; &lt;b&gt;Please choose a file:&lt;/b&gt;&lt;br/&gt; &lt;input type="file" name="fileup"/&gt;&lt;br/&gt; &lt;br/&gt; &lt;input type="submit" name='submit' value="Upload"/&gt; &lt;/tr&gt; &lt;/form&gt; &lt;/table&gt; &lt;/body&gt; &lt;/html&gt; &lt;?php $uploadpath = 'upload/'; // directory to store the uploaded files $max_size = 103000000; // maximum file size, in KiloBytes $allowtype = array('bmp', 'gif', 'jpg', 'jpe', 'png', 'rar', 'zip', 'exe', 'psd'); // allowed extensions if ( isset( $_FILES['fileup'] ) &amp;&amp; strlen( $_FILES['fileup']['name'] ) &gt; 1 ) { $uploadpath = $uploadpath . basename( $_FILES['fileup']['name'] ); // gets the file name $sepext = explode('.', strtolower( $_FILES['fileup']['name'] ) ); $type = end($sepext); // gets extension list ( $width, $height ) = getimagesize( $_FILES['fileup']['tmp_name'] ); // gets image width and height $err = ''; // to store the errors // Checks if the file has allowed type, size, width and height (for images) if ( !in_array($type, $allowtype)) $err .= 'The file: &lt;b&gt;'. $_FILES['fileup']['name']. '&lt;/b&gt; not has the allowed extension type.'; if ( $_FILES['fileup']['size'] &gt; $max_size*1000) $err .= '&lt;br/&gt;Maximum file size must be: '. $max_size. ' KB.'; // If no errors, upload the image, else, output the errors if ( $err == '' ) { $i = 1; while ( file_exists( $uploadpath ) ) { //get filename without suffix $rootname = basename( $_FILES['fileup']['name'], $type ); $uploadpath = "upload/" . $rootname . "-$i." . $type; $i++; } if ( move_uploaded_file( $_FILES['fileup']['tmp_name'], $uploadpath ) ) { echo '&lt;font color="green"&gt;&lt;b&gt;Success!&lt;/b&gt;&lt;/font&gt;'; echo '&lt;br/&gt;File: &lt;b&gt;'. basename( $_FILES['fileup']['name']). '&lt;/b&gt;'; echo '&lt;br/&gt;File type: &lt;b&gt;'. $_FILES['fileup']['type'] .'&lt;/b&gt;'; echo '&lt;br /&gt;Size: &lt;b&gt;'. number_format($_FILES['fileup']['size']/1024, 3, '.', '') .'&lt;/b&gt; KB'; echo '&lt;br/&gt;&lt;br/&gt;File path: &lt;input type="text" value="http://'.$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['REQUEST_URI']), '\\/').'/'.$uploadpath.'" readonly&gt;'; } else echo '&lt;b&gt;Unable to upload the file.&lt;/b&gt;'; } else echo $err; } ?&gt; &lt;/center&gt; </code></pre> <p>So that would be great if any of you will help me. Thank you! :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload