Is it possible to have something like the following
<form id="uploadForm" action="" method="post" enctype="multipart/form-data">
<p>Upload File 1</p>
<input type="file" name="profile"/>
<p>Upload File 2</p>
<input type="file" name="cover"/>
<input type="submit" value="Submit" />
</form>
I then have some php script looking like:
if (empty($_POST['save']) === false) {
// FOR PROFIL CHANGE
if (isset($_FILES['profile']) === true){
$allowed= array('jpg', 'jpeg', 'png', 'bmp');
$file_name = $_FILES['profile']['name']; //name of the file
$file_exts = explode('.', $file_name); // extension of the file
$file_extn = strtolower(end($file_exts)); //inlowercase
$file_temp = $_FILES['profile']['tmp_name'];
$id = $user_data['id'];
change_image2($id, $file_temp, $file_extn);
}
// FOR COVER CHANGE
if (isset($_FILES['cover']) === true){
$allowed= array('jpg', 'jpeg', 'png', 'bmp');
$file_name = $_FILES['cover']['name']; //name of the file
$file_exts = explode('.', $file_name); // extension of the file
$file_extn = strtolower(end($file_exts)); //inlowercase
$file_temp = $_FILES['cover']['tmp_name'];
$id = $user_data['id'];
change_image3($id, $file_temp, $file_extn);
}
But if I upload just one file ( cover for example ); it is saved also in profile for some reason ... If find this weird because i gave different names to the inputs. Can anybody explain the problem please?
Use print_r($_FILES)
to check what data you receive when only one file is uploaded.
I think $_FILES['profile']
is always set, no matter if a file is uploaded using the corresponding <INPUT>
element or not. You should check if $_FILES['profile']['name']
contains the file name or is empty.
You must also use is_uploaded_file()
(and move_uploaded_file()
) with $_FILES['profile']['tmp_name']
to handle the file.
is_uploaded_file()
is the only authoritative answer to the question: "did the user uploaded a file using this <input>
control?"
// FOR PROFIL CHANGE
if (! empty($_FILES['profile']['name'])
&& is_uploaded_file($_FILES['profile']['tmp_name'])){
// ... process the file ...
Change your condition from if (isset($_FILES['profile']) === true){
into if (strlen($_FILES['profile']['tmp_name']) > 0){
. There will be always $_FILES['profile']
, but $_FILES['profile']['tmp_name']
contains some data only if there is some file transfer.