Check file input on PHP and JQuery. Why Error?
JQuery code client.
Add an form tag with the following attributes.
<form enctype="multipart/form-data" method="post"></form>
Add an input tag with the following attributes.
<form enctype="multipart/form-data" method="post"> <input id="" type="file" name="myfile"> </form>
And the submit button.
<form enctype="multipart/form-data" method="post"> <input id="" type="file" name="myfile"> <input type = "submit" value = "OK"> </form>
PHP code server.
Using the global array $_FILES.
$name_file_avatar = $_FILES['myfile']['name'];//name file picter $tmp_dir = $_FILES['myfile']['tmp_name'];//temporary file storage directory
Use the move_uploaded_file() function to move the file to another directory
$destiation_dir = '/var/www/Your_site/download/'.$name_file_avatar; move_uploaded_file( $tmp_dir, $destiation_dir );
Complete code example.
<?php $name_file_avatar = $_FILES['myfile']['name']; $tmp_dir = $_FILES['myfile']['tmp_name']; $destiation_dir = '/var/www/your_site/user/download/'.$name_file_avatar; move_uploaded_file( $tmp_dir, $destiation_dir ); // Move the file to the desired directory if ( !$name_file_avatar ) $name_file_avatar = "no_avatar.jpg"; echo '<DIV class="custom_code"> <IMG src="/user/download/'.$name_file_avatar.'" /> <form enctype="multipart/form-data" method="post"> <P><input id="avatar_spage" type="file" name="myfile"></P> <P><input type = "submit" value = "OK"></P> </form> </DIV>'; ?>
File check. How to upload pictures only.
File check jQuery and JS on client.
$("#avatar_spage").change(function(){ var file = document.getElementById("avatar_spage").files[0]; if ( !(file.type=="image/jpeg" || file.type=="image/png" || file.type=="image/gif" || file.type=="image/jpg") ) { alert("Invalid file. Please select a file in the format jpeg, png, gif, jpg."); $("#avatar_spage").val("");//clear input file } });
File check PHP on server.
$name_file_avatar = $_FILES['myfile']['name']; $FileExtension = substr($name_file_avatar, strrpos($name_file_avatar, '.') + 1); if ( $FileExtension=="PNG" || $FileExtension=="jpg" || $FileExtension=="jpeg" || $FileExtension=="gif" || $FileExtension=="png" ) { if(isset($_FILES) && $_FILES['myfile']['error'] == 0){ // Check if the user has uploaded the file. $tmp_dir = $_FILES['myfile']['tmp_name']; // Temporary directory. $destiation_dir = '/var/www/your_site/user/download/'.$name_file_avatar; move_uploaded_file($tmp_dir, $destiation_dir ); } else{ echo '<P align=CENTER>No File Uploaded</P>'; } } else { if ( isset($_FILES) ) echo "<P align=CENTER>This is not a picture!!! File not loaded!</P>"; }
Full custom code.
<?php $name_file_avatar = $_FILES['myfile']['name']; $FileExtension = substr($name_file_avatar, strrpos($name_file_avatar, '.') + 1); if ( $FileExtension=="PNG" || $FileExtension=="jpg" || $FileExtension=="jpeg" || $FileExtension=="gif" || $FileExtension=="png" ) { if(isset($_FILES) && $_FILES['myfile']['error'] == 0){ $tmp_dir = $_FILES['myfile']['tmp_name']; $destiation_dir = '/var/www/spage.me/html/user/download/'.$name_file_avatar; move_uploaded_file($tmp_dir, $destiation_dir ); } else { echo '<P align=CENTER>No File Uploaded</P>'; } } else { if ( isset($_FILES) ) echo "<P align=CENTER>This is not a picture!!! File not loaded!</P>"; } if ( !$name_file_avatar ) $name_file_avatar = "no_avatar.jpg"; echo '<DIV class="custom_code"> <IMG src="/user/download/'.$name_file_avatar.'" /> <form enctype="multipart/form-data" method="post"> <P><input id="avatar_spage" type="file" name="myfile" accept="jpeg,png,gif,jpg"></P> <P><input type = "submit" name = "submit" value = "OK"></P> </form> </DIV> <SCRIPT> $("#avatar_spage").change(function(){ var file = document.getElementById("avatar_spage").files[0]; if ( !(file.type=="image/jpeg" || file.type=="image/png" || file.type=="image/gif" || file.type=="image/jpg") ) { alert("Invalid file. Please select a file in the format jpeg, png, gif, jpg."); $("#avatar_spage").val("");//clear input file } }); </SCRIPT>'; ?>
Error “failed to open stream: Permission denied in”.
- Know the error code php. Place this code at the beginning of the php file.
<?php ini_set('error_reporting', E_ALL); ini_set('display_errors', 1); ini_set('display_startup_errors', 1); //... custom code ... ?>
2. Change file attributes
Check the attributes on the file relocation directory.
The folder must be accessed 777. (/var/www/your_site/user/download).
2. Check the form tag attribute.
The form tag must have an attribute enctype=”multipart/form-data” method=”post“.
3. Look at the array of the global variable $_FILES. It will help you.
In the array you will find the file transfer error code.
echo '<pre>Some debug information:'; print_r ($_FILES); echo "</pre>";
4. Look at the file parameters on the client. It will help you.
<SCRIPT> var file = document.getElementById("avatar_spage").files[0]; ext = "no data"; parts = file.name.split('.'); if (parts.length > 1) ext = parts.pop(); var a = "File size: " + file.size + " B "; var b = "File extension: " + ext + " "; var c = "MIME type: " + file.type; alert(a+b+c); </SCRIPT>
5. The directory must have the correct user and user group rights. Make sure of this.
Commands for the server ubuntu/linux on ssh.
cd /var/www/your_site/user
ls -l