I have created an add file upload function and multiple file upload function.
Add function is working fine, but Edit function is not working.
After submitting the EDIT form, the OLD VALUES are not saved in the database.
The file is saved in some other folder.
Kindly help.
this is my code:controller file:
if(isset($_POST['additionalimage_exists']) && $_POST['additionalimage_exists'] != ''){
$temp_image = $_POST['additionalimage_exists'];
//echo $temp_image;die;
if(isset($_FILES['additional_images']['name']) && $_FILES['additional_images']['name'] != ""){
$errors= array();
$additional_eventimages = $_FILES['additional_images']['name'];
foreach($_FILES['additional_images']['name'] as $key => $tmpName) {
$additionalimages = $_FILES['additional_images']['name'][$key];
$file_type = $_FILES['additional_images']['type'][$key];
$file_size = $_FILES['additional_images']['size'][$key];
$file_tmp = $_FILES['additional_images']['tmp_name'][$key];
move_uploaded_file($file_tmp,"images/events/".$additionalimages);
}
}
else
{
$additional_eventimages = $temp_image;
}
}
view File:
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="additional-image">Additional Images</label>
<input type="file" class="form-control" name="additional_images[]" value="<?php echo $additional_images;?>" multiple="multiple">
<input type="hidden" name="additionalimage_exists" value="<?php echo $additional_images;?>" class="form-control-file" id="exampleInputFile" placeholder="Enter Image Text" aria-describedby="fileHelp">
<div>
<?php echo $additional_images;?>
</div>
</div>
</div>
</div>
Try with this code.
if(isset($_FILES['additional_images']['name']) && $_FILES['additional_images']['name'] != ""){
$errors= array();
$additional_eventimages = $_FILES['additional_images']['name'];
foreach($_FILES['additional_images']['name'] as $key => $tmpName) {
$additionalimages = $_FILES['additional_images']['name'][$key];
$file_type = $_FILES['additional_images']['type'][$key];
$file_size = $_FILES['additional_images']['size'][$key];
$file_tmp = $_FILES['additional_images']['tmp_name'][$key];
move_uploaded_file($file_tmp,"images/events/".$additionalimages);
} else if(isset($_POST['additionalimage_exists']) && $_POST['additionalimage_exists'] != ''){
$additional_eventimages = $_POST['additionalimage_exists'];
} else {
$additional_eventimages = "";
}
Related
My web app only allows for one file upload which is an issue because users want to be able to submit 2 or more files when necessary.
Please how do I allow multiple file uploads and displaying those files to be downloaded by another type of users?
MODEL
public function upload_docs($data)
{
$this->db->where('homework_id',$data['homework_id']);
$this->db->where('student_id',$data['student_id']);
$q = $this->db->get('submit_assignment');
if ( $q->num_rows() > 0 )
{
$this->db->where('homework_id',$data['homework_id']);
$this->db->where('student_id',$data['student_id']);
$this->db->update('submit_assignment',$data);
} else {
$this->db->insert('submit_assignment',$data);
}
}
CONTROLLER
public function upload_docs()
{
$homework_id = $_REQUEST['homework_id'];
$student_id =$_REQUEST['student_id'];
$data['homework_id'] = $homework_id;
$data['student_id'] = $student_id;
$data['message'] = $_REQUEST['message'];
// $data['id']=$_POST['assigment_id'];
$is_required=$this->homework_model->check_assignment($homework_id,$student_id);
$this->form_validation->set_rules('message', $this->lang->line('message'), 'trim|required|xss_clean');
$this->form_validation->set_rules('file', $this->lang->line('attach_document'), 'trim|xss_clean|callback_handle_upload['.$is_required.']');
if ($this->form_validation->run() == FALSE) {
$msg=array(
'message'=>form_error('message'),
'file'=>form_error('file'),
);
$array = array('status' => 'fail', 'error' => $msg, 'message' => '');
}else{
if (isset($_FILES["file"]) && !empty($_FILES['file']['name'])) {
$time = md5($_FILES["file"]['name'] . microtime());
$fileInfo = pathinfo($_FILES["file"]["name"]);
$img_name = $time . '.' . $fileInfo['extension'];
$data['docs'] = $img_name;
move_uploaded_file($_FILES["file"]["tmp_name"], "./uploads/homework/assignment/" . $data['docs']);
$data['file_name']=$_FILES["file"]['name'];
$this->homework_model->upload_docs($data);
}
$array = array('status' => 'success', 'error' => '', 'message' => $this->lang->line('success_message'));
}
echo json_encode($array);
}
public function handle_upload($str,$is_required)
{
$image_validate = $this->config->item('file_validate');
if (isset($_FILES["file"]) && !empty($_FILES['file']['name']) && $_FILES["file"]["size"] > 0) {
$file_type = $_FILES["file"]['type'];
$file_size = $_FILES["file"]["size"];
$file_name = $_FILES["file"]["name"];
$allowed_extension = $image_validate['allowed_extension'];
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
$allowed_mime_type = $image_validate['allowed_mime_type'];
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mtype = finfo_file($finfo, $_FILES['file']['tmp_name']);
finfo_close($finfo);
if (!in_array($mtype, $allowed_mime_type)) {
$this->form_validation->set_message('handle_upload', 'File Type Not Allowed');
return false;
}
if (!in_array($ext, $allowed_extension) || !in_array($file_type, $allowed_mime_type)) {
$this->form_validation->set_message('handle_upload', 'Extension Not Allowed');
return false;
}
if ($file_size > $image_validate['upload_size']) {
$this->form_validation->set_message('handle_upload', $this->lang->line('file_size_shoud_be_less_than') . number_format($image_validate['upload_size'] / 1048576, 2) . " MB");
return false;
}
return true;
} else {
if($is_required==0){
$this->form_validation->set_message('handle_upload', 'Please choose a file to upload.');
return false;
}else{
return true;
}
}
}
VIEW
<form id="upload" role="form" method="post" class="ptt10" enctype="multipart/form-data" action="upload_docs">
<div class="modal-body pt0">
<div class="row">
<input type="hidden" name="student_id" value="<?php echo $student_id; ?>">
<input type="hidden" id="homework_id" name="homework_id">
<input type="hidden" id="assigment_id" name="assigment_id">
<div class="col-sm-12">
<div class="form-group">
<label for="pwd"><?php echo $this->lang->line('message'); ?></label>
<textarea type="text" id="assigment_message" name="message" class="form-control "></textarea>
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<label for="pwd"><?php echo $this->lang->line('attach_document'); ?></label>
<input type="file" id="file" name="file" class="form-control filestyle">
</div>
</div>
<p id="uploaded_docs"></p>
</div>
</div>
<div class="box-footer">
<div class="" id="footer_area">
<button type="submit" form="upload" class="btn btn-info pull-right" id="submit" data-loading-text='Please wait...'><?php echo $this->lang->line('save'); ?></button>
</div>
</div>
</form>
This is displayed like this
controller
public function assigmnetDownload($doc)
{
$this->load->helper('download');
$name = $this->uri->segment(5);
$ext = explode(".", $name);
$filepath = "./uploads/homework/assignment/" . $doc;
$data = file_get_contents($filepath);
force_download($name, $data);
}
view
<table class="table table-hover table-striped table-bordered example">
<thead>
<tr>
<th><?php echo $this->lang->line('name') ?></th>
<th><?php echo $this->lang->line('message') ?></th>
<th class="text-right"><?php echo $this->lang->line('action') ?></th>
</tr>
</thead>
<tbody id="homework_docs_result">
</tbody>
</table>
Thanks to all helping me out. I did as you guys directed. However, I noticed I'm able to select multiple files now but gets stuck on the save button.
When I click, nothing happens. Data is not submitted.
This is what I have done so far
controller
if (isset($_FILES["file"])){
foreach($_FILES["file"] as $file){
if(!empty($file["name"])){
$time = md5($file["file"]['name'] . microtime());
$fileInfo = pathinfo($file["file"]["name"]);
$img_name = $time . '.' . $fileInfo['extension'];
$data['docs'] = $img_name;
move_uploaded_file($file["file"]["tmp_name"], "./uploads/homework/assignment/" . $data['docs']);
$data['file_name']=$file["file"]['name'];
$this->homework_model->upload_docs($data);
}
$array = array('status' => 'success', 'error' => '', 'message' => $this->lang->line('success_message'));
}
echo json_encode($array);
}
}
}
view
<form id="upload" role="form" method="post" class="ptt10" enctype="multipart/form-data" action="uploaded_docs">
<input type="file" multiple="" id="file" name="file[]" class="form-control filestyle">
<button type="submit" form="upload" class="btn btn-info pull-right" id="submit" data-loading-text=' Please wait'><?php echo $this->lang->line('save'); ?></button>
</form>
HTML
<input type="file" id="file" name="file[]" class="form-control filestyle">
Setting name to file[] will accept array of files
PHP
if (isset($_FILES["file"]){
foreach($_FILES["file"] as $file){ //this loop will get one file from 'file[]' array at a time
if(!empty($file["name"])){
//your alreay written code
//replace $_FILES["file"] with $file
}
}
}
Hope this helpful :)
Some tips, if help. Do not forget the vote to strengthen. Let's go...
<form method="post" action="upload_docs" enctype="multipart/form-data">
<input name="filesToUpload[]" type="file" multiple="" />
</form>
On your controller it's simple ...
if(isset($_FILES['filesToUpload'])) {
foreach ($_FILES['filesToUpload'] as $fileUp) {
$time = md5($fileUp["file"]['name'].microtime());
$fileInfo = pathinfo($fileUp["file"]["name"]);
$img_name = "{$time}.{$fileInfo['extension']}";
$data['docs'] = $img_name;
move_uploaded_file($fileUp["file"]["tmp_name"], "./uploads/homework/assignment/{$data['docs']}");
$data['file_name']=$fileUp["file"]['name'];
$this->homework_model->upload_docs($data);
}
}
To display multiple files you need to use the same concept as uploading working with
foreach($files as $file){ ... }
I hope I helped. Success!
Setting attribute multiple will allow selecting multiple files at once by pressing ctrl, setting the name as an array(file[]) will allow more than files name to be stored.
<input type="file" id="file" name="file[]" class="form-control filestyle" multiple>
However, if you don't want the user to upload multiple files at once then you'll have to make multiple input fields with the same name as an array(file[])
<input type="file" id="file" name="file[]" class="form-control filestyle">
<input type="file" id="file1" name="file[]" class="form-control filestyle">
...
...
In your controller, you'll have to traverse through each element as it is now an array.
foreach($_FILES["file"]['name'] as $file){ //single element
echo $file; // returns the name of file
// your-logic-to-upload
}
If you're having trouble uploading multiple files, see here, here and here.
Hope it helps you.
code:
<?php
if(isset($_POST['submit']))
{
if(isset($_FILES['img_url']['name']))
{
for($i=0; $i<count($_FILES['img_url']['name']); $i++)
{
$tmpFilePath = $_FILES['img_url']['tmp_name'][$i];
if ($tmpFilePath != "")
{
$path = "../images/hotel_images/";
$name = $_FILES['img_url']['name'][$i];
$size = $_FILES['img_url']['size'][$i];
list($txt, $ext) = explode(".", $name);
$file= time().substr(str_replace(" ", "_", $txt), 0);
$info = pathinfo($file);
$filename = $file.".".$ext;
$filepath_upload = $path.$filename;
$imageFileType = strtolower(pathinfo($filename,PATHINFO_EXTENSION));
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" )
{
$j2 = $i+1;
$msg = "<div class='alert alert-success'>Sorry, ".$j2." Image files Extension .".$ext." are not Uploaded & Other files Uploaded Successfully <br/>.</div>" ;
}
else if(move_uploaded_file($_FILES['img_url']['tmp_name'][$i], $filepath_upload))
{
$banner_image_source_file = $filepath_upload;
$banner_image_save_file = $filepath_upload;
list($width_orig, $height_orig) = getimagesize($banner_image_source_file);
$img_url_name.=$filename."|";
}
}
$filepath = rtrim($img_url_name, '|');
}
if(!empty($filepath))
{
$query = mysqli_query($con, "INSERT INTO `hotel`(`img_url`, `status`) VALUES ('".$filepath."')");
if($query)
{
$msg .= "<div class='alert alert-success'>Hotel Record Added Successfully</div>";
}
else
{
$msg = "<div class='alert alert-success'>Unbale to Add Hotel Record Please Try Again !!!</div>";
}
}
else
{
$msg = "<div class='alert alert-success'>Unable to Add Please Try Again !! </div>";
}
}
else
{
$msg = "<div class='alert alert-success'>Unable to Add Please Try Again !! </div>";
}
}
?>
<form action="" method="POST" enctype="multipart/form-data" id="add-hotel-form">
<div class="form-group col-md-12">
<label for="hotel">Upload Hotel Images</label>
<input type="file" class="form-control-file" id="upload_hotel_img1" name="img_url[]" multiple>
</div>
<div class="form-group col-md-12">
<label for="room">Upload Room Images</label>
<input type="file" class="form-control-file" id="upload_room_img1" name="img_url2[]" multiple>
</div>
<input type="submit" class="btn btn-primary" name="submit" value="Submit">
</form>
In the following code I have create a simple form where I have two input field type='file' where I am able to upload only first one i.e. <label for="hotel">Upload Hotel Images</label> and move into the folder successfully but I also want to upload and move second input field i.e. <label for="room">Upload Room Images</label>. I don't have any idea about this. So, How can I do this? Please help me.
Thank You
Your Room Image input is called img_url2, which you never interact with in code.
For best practice, move the bulk of your PHP code into a file upload function, then call it once with img_url $_FILE input, and once with img_url2.
Example:
<?php
error_reporting(E_ALL);
ini_set("display_errors", true);
ini_set("display_startup_errors", true);
function uploadFile($file, $path)
{
$img_url_name = "";
if(is_array($file) && isset($file['name']) && isset($file['tmp_name']) && isset($file['size']))
{
$tmpFilePath = isset($file['tmp_name'][0]) ? $file['tmp_name'][0] : "";
$name = isset($file['name'][0]) ? $file['name'][0] : "";
list($txt, $ext) = explode(".", $name);
$file = time().substr(str_replace(" ", "_", $txt), 0);
$filename = $file.".".$ext;
$filepath_upload = $path.$filename;
$imageFileType = strtolower(pathinfo($filename,PATHINFO_EXTENSION));
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" )
{
return "<div class='alert alert-success'>Sorry, ".$name." Image files Extension .".$ext." are not Uploaded & Other files Uploaded Successfully <br/>.</div>" ;
}
else if(move_uploaded_file($tmpFilePath, $filepath_upload))
{
$img_url_name .= $filename."|";
}
$filepath = rtrim($img_url_name, '|');
if(!empty($filepath))
{
if(!isset($con))
{
global $con;
}
$query = mysqli_query($con, "INSERT INTO `hotel`(`img_url`, `room_url`) VALUES ('".$filename."','".$filepath."')");
if($query)
{
return "<div class='alert alert-success'>Hotel Record Added Successfully</div>";
}
}
}
return "<div class='alert alert-success'>Unable to Add Please Try Again !! </div>";
}
if(isset($_POST['submit']))
{
if(isset($_FILES['img_url']['name']))
{
echo uploadFile($_FILES['img_url'], "../images/hotel_images/");
}
if(isset($_FILES['img_url2']['name']))
{
echo uploadFile($_FILES['img_url2'], "../images/room_img/");
}
}
?>
<form action="" method="POST" enctype="multipart/form-data" id="add-hotel-form">
<div class="form-group col-md-12">
<label for="hotel">Upload Hotel Images</label>
<input type="file" class="form-control-file" id="upload_hotel_img1" name="img_url" multiple>
</div>
<div class="form-group col-md-12">
<label for="room">Upload Room Images</label>
<input type="file" class="form-control-file" id="upload_room_img1" name="img_url2" multiple>
</div>
<input type="submit" class="btn btn-primary" name="submit" value="Submit">
</form>
I use FormData() to upload multiple images from multiple fields. Please take a look at this code:
<head>
<title>Example</title>
</head>
<body>
<form enctype="multipart/form-data" id="add-hotel-form">
<div class="form-group col-md-12">
<label for="hotel">Upload Hotel Images</label>
<input type="file" class="form-control-file" id="upload_hotel_img1" name="img_url" multiple>
</div>
<div class="form-group col-md-12">
<label for="room">Upload Room Images</label>
<input type="file" class="form-control-file" id="upload_room_img2" name="img_url2" multiple>
</div>
<input type="button" class="btn btn-primary" name="submit" value="Submit" onclick="myfunction();">
</form>
</body>
<script>
function myfunction(){
var form_data = new FormData();
var image = document.getElementById('upload_hotel_img1').files.length;
var images = document.getElementById('upload_room_img2').files.length;
for (var x = 0; x < image; x++) {
form_data.append("image", document.getElementById('upload_hotel_img1').files[x]);
}
for (var x = 0; x < images; x++) {
form_data.append("images[]", document.getElementById('upload_room_img2').files[x]);
}
}
</script>
We can retrieve these images in the backend using the follwing : $_FILES["image"]['name'] and $_FILES["images"]['name'] respectively.
I am on Ubuntu. I am trying to take user file upload of small images. I checked the $_FILES it's filled with data. I tried to debug the move command but it doesnot echo anything.
if ($_SERVER['REQUEST_METHOD'] == 'POST' ){
//Now handle everything
if(isset($_POST["submit"])) {
print_r($_FILES);
//Store the image
if(!empty($_FILES['uploaded_file']))
{
$path = "/neel/public/img/";
$path = $path.basename($_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
echo 'Its working';
} else{
echo 'I am done!!!';
die();
}
}
createnewEvent($conn);
header('Location:/neel/index.php');
}
}
You can check if the file exists by checking its name.
if(!empty($_FILES['file']['name']))
Where file is the name of input field for file.
P. G above here is correct.
Instead of checking, if $_POST['submit']
You should check this:
if(isset($_FILES['uploaded_file']['name']))
Try this code it's a complete sign up form with PHP , Bootstrap
HTML
<div class="container">
<div class="row">
<br>
<h1 class="text-center">Create new account</h1>
<br>
<div class="col-lg-4 col-md-6 col-sm-12">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
<div class="form-group">
<input type="text" class="form-control form-control-lg" name="name" placeholder="Your Name">
</div>
<div class="form-group">
<input type="text" class="form-control form-control-lg" name="username" placeholder="Username">
</div>
<div class="form-group">
<input type="password" class="form-control form-control-lg" name="password" placeholder="Password">
</div>
<div class="form-group text-center">
<div class='file-input'>
<input type='file' name="profile-img">
<span class='button label' data-js-label>Choose your profile image</span>
</div>
</div>
<div class="form-group">
<input type="submit" class="btn btn-success form-control form-control-lg" name="signup" value="Signup">
</div>
</form>
</div>
</div>
</div>
PHP
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$errors;
if (empty($_POST['name'])) {
$errors[] = "Name field is empty";
}
if (empty($_POST['username'])) {
$errors[] = "Username field is empty";
}
if (empty($_POST['password'])) {
$errors[] = "Password field is empty";
}
if (empty($_FILES['profile-img'])) {
$errors[] = "You should upload profile image";
} else{
$img = $_FILES['profile-img'];
$ext = end(explode('.', $img['name']));
$allowed_extensions = array('jpg', 'jpeg', 'png', 'gif');
$max_size = 4; //MegaBytes
if (! in_array($ext, $allowed_extensions)) {
$errors[] = "Please , Choose a valid image";
}
$img_size = $img['size'] / 1000000; // By Megabyte
if ($img_size > $max_size) {
$errors[] = "This picture is so large";
}
}
if (!empty($errors)) {
echo '<div class="container">';
foreach ($errors as $error) {
echo '
<div class="alert alert-danger" role="alert">
' . $error . '
</div>
';
}
echo "</div>";
} else {
$username = filter_var(htmlentities(trim($_POST['username'])), FILTER_SANITIZE_STRING);
$name = filter_var(htmlentities(trim($_POST['name'])), FILTER_SANITIZE_STRING);
$password = sha1(filter_var(htmlentities(trim($_POST['password'])), FILTER_SANITIZE_STRING));
// Profile Picture :-
$imgname = uniqid(uniqid()) . #date("Y-m-d") . "." . $ext;
$target_bath = "uploads/imgs/";
$target_bath = $target_bath . basename($imgname);
$orginal_img = $img['tmp_name'];
if (move_uploaded_file($orginal_img, $target_bath)) {
$sql = "INSERT INTO users(name, username, password,profile_picture, r_d) VALUES ('$name', '$username', '$password', '$target_bath', NOW())";
$result = mysqli_query($conn, $sql);
header('location: ./login.php');
}
}
}
The script you've shown shown will only "not echo anything" if $_SERVER['REQUEST_METHOD'] is not "POST". Assuming your description of events is accurate, then the problem is in the form #halojoy has asked that you show here.
I do hope that you are not redirecting the script back to itself. Also you shouldn't attempt to do a redirect after an echo.
Php code
$target = "upload/";
$nameF = "";
$targetImage = "upload/";
$nameI = "";
if (!empty($_FILES['fileUP']['name'])) {
print_r("ce il file");
$target = $target . basename($_FILES['fileUP']['name']);
$nameF = $_FILES['fileUP']['name'];
if (!move_uploaded_file($_FILES['fileUP']['tmp_name'], $target)) {
echo -1;
}
}
if (!empty($_FILES['imageUP']['name'])) {
$targetImage = $targetImage . basename($_FILES['imageUP']['name']);
$nameI = $_FILES['imageUP']['name'];
if (!move_uploaded_file($_FILES['imageUP']['tmp_name'], $targetImage)) {
echo -1;
}
}
$title = $_POST['title'];
$admin = $_POST['admin'];
$content = $_POST['content'];
$sql = "INSERT INTO news (title,admin,content,img,file) values('$title','$admin','$content','$nameI','$nameF')";
$result = $conn->query($sql) or die(mysql_error());
if ($result === TRUE) {
echo 1;
} else {
echo -1;
}
Form
<form enctype="multipart/form-data" id="insert" class="bs-example bs-example-form" method="POST">
<div class="input-group">
<span class="input-group-addon">Titolo</span>
<input id="title" name="title" type="text" class="form-control" placeholder="Titolo">
</div>
<br>
<div class="input-group">
<span class="input-group-addon">Admin</span>
<input id="admin" name="admin" type="text" class="form-control" value='{{$utente|lower}}'
placeholder='{{$utente}}'>
</div>
<br><br> <br> <br>
<div class="input-group">
<span class="input-group-addon">Immagine</span>
<input id="image" name="imageUP" accept="image/*" type="file" class="form-control"
placeholder="Immagine">
</div>
<br>
<div class="input-group">
<span class="input-group-addon">File</span>
<input id="image" name="fileUP" id="fileToUpload" type="file" class="form-control"
placeholder="FIle">
</div>
<br>
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-font"></span></span>
<input id="content" name="content" type="text" class="form-control"
placeholder="Contenuto">
</div>
<br>
<button id="crea" type="submit" class="btn btn-warning">Crea</button>
</form>
Ajax request
$('#insert').submit(function (e) {
e.preventDefault();
var data = new FormData($(this)[0]);
$.ajax
({
url: 'uploads.php',
data: data,
type: 'post',
processData: false,
contentType: false,
success: function (response) {
response = parseInt(response);
switch (response) {
case -1: //errore generico
alert("errore");
break;
case 1:
alert("la creazione della news è andata a buon fine");
break;
}
close ajax call..
My problem is:
the script work but not well, i've notice that query don't insert data if i put text in the 'content' input and upload image.
In the console I've this error:
not allowed to load local resource: file:///C:/fakepath/xx.jpg
when i work in localhost i haven't this error and query ALWAYS insert data. Now i've problem and i am in real server.
Anyone know to fix it?ù
I need your help
Try this. And, let me know. Use whole code as it is. I will explain in few minutes. First try.
$target = "upload/";
$nameF = "";
$targetImage = "upload/";
$nameI = "";
$flag = 1;
if (!empty( $_FILES['fileUP']['name'])) {
print_r("ce il file");
$target = $target . basename( $_FILES['fileUP']['name']);
$nameF =$_FILES['fileUP']['name'];
if (!move_uploaded_file($_FILES['fileUP']['tmp_name'], $target)) {
$flag = -1;
}
}
if (!empty( $_FILES['imageUP']['name'])) {
$targetImage = $targetImage . basename( $_FILES['imageUP']['name']);
$nameI =$_FILES['imageUP']['name'];
if (!move_uploaded_file($_FILES['imageUP']['tmp_name'], $targetImage)) {
$flag = -1;
}
}
$title = $_POST['title'];
$admin = $_POST['admin'];
$content = $_POST['content'];
$sql = "INSERT INTO news (title,admin,content,img,file) values('$title','$admin','$content','$nameI','$nameF')";
$result = $conn->query($sql) or die(mysql_error());
if ($result === TRUE) {
$flag = 1;
}
else {
$flag = -1;
}
if($flag == -1){
echo -1;
} else {
echo 1;
}
Below is a php function of my post method. I am not receiving any errors but however it is not uploading into the folder I have assigned to.
I am using a localhost and there are no errors regarding the database!
<div id="box">
<form method='post' enctype="multipart/form-data">
<?php
if(isset($_FILES['video'])){
$name = $_FILES['video']['name'];
$type = explode('.', $name);
$type = end($type);
$size = $_FILES['video']['name'];
$random_name = rand();
$tmp = $_FILES['video']['tmp_name'];
if($type != 'mp4' && $type != 'mp4' && $type != 'flv'){
$message = "Video Format Not Supported!";
}
else {
move_uploaded_file($tmp, 'videos/'.$random_name.'.'.$type);
mysql_query("INSERT INTO videos VALUES('.' '$name'; 'videos/$random_name.$type')");
$message = "Successfully Uploaded!";
}
echo "$message";
}
?>
Select Video : <br/>
<input type='file' name="video" />
<br/><br/>
<input type="submit" value="Upload" />
</form>