I created a new function to change the name of the file to be uploaded. Now I have successfully changed the file name. but in my code, before it loops through the files normally it gives an error.
I have 5 files inputs where the input is in 1 form.
There was no problems before adding my function, but after adding the rename function, my loop became messy, but my old code was working correctly.
here is my old code:
public function proses_upload()
{
$gambar = array();
$jumlah = count($_FILES['userfile']['name']);
for ($i=0; $i < $jumlah; $i++)
{
$file_name = $_FILES['userfile']['name'][$i];
$tmp_name = $_FILES['userfile']['tmp_name'][$i];
move_uploaded_file($tmp_name, "file/".$file_name);
$gambar[$i] = $file_name;
}
$nama_file1 = $gambar[0];
$format_p2ptm =1;
$tanggal = date("Y-m-d H:i:s");
$jenis ='p2ptm';
$nama_file2 = $gambar[1];
$format_p2ptm2 =2;
$jenis ='p2ptm';
$nama_file3 = $gambar[2];
$format_p2ptm3 =3;
$jenis ='p2ptm';
$nama_file4 = $gambar[3];
$format_p2ptm4 =4;
$jenis ='p2ptm';
$nama_file5 = $gambar[4];
$format_p2ptm5 =5;
$jenis ='p2ptm';
if (!$nama_file1 == "") {
$this->app_model->insert_data('penyakit', $format_p2ptm, $tanggal,$nama_file1,$jenis, $this->session->userdata('id_puskesmas'));
}
if(!$nama_file2 == ""){
$this->app_model->insert_data('penyakit', $format_p2ptm2, $tanggal,$nama_file2,$jenis,$this->session->userdata('id_puskesmas'));
}
if(!$nama_file3 == ""){
$this->app_model->insert_data('penyakit', $format_p2ptm3, $tanggal,$nama_file3,$jenis,$this->session->userdata('id_puskesmas'));
}
if(!$nama_file4 == ""){
$this->app_model->insert_data('penyakit', $format_p2ptm4, $tanggal,$nama_file4,$jenis,$this->session->userdata('id_puskesmas'));
}
if(!$nama_file5 == ""){
$this->app_model->insert_data('penyakit', $format_p2ptm5, $tanggal,$nama_file5,$jenis,$this->session->userdata('id_puskesmas'));
}
redirect('puskesmas/view_puskesmas');
}
Here is my new code with errors:
public function proses_upload()
{
$gambar = array();
$jumlah = count($_FILES['userfile']['name']);
for ($i=0; $i < $jumlah; $i++)
{
$file_name = $_FILES['userfile']['name'][$i];
$explode = explode('.',$file_name);
// die(print_r($explode));
$ekstensi = pathinfo($file_name, PATHINFO_EXTENSION);
$bulan = date('M');
$tahun = date('y') ;
$kode = $this->session->userdata('kode_puskesmas');
$new_file_name = $explode[1].'_'.$kode.'_'.$bulan.$tahun.'.'.$ekstensi;
$tmp_name = $_FILES['userfile']['tmp_name'][$i];
move_uploaded_file($tmp_name, "file/".$new_file_name);
$gambar[$i] = $new_file_name;
}
$nama_file1 = $gambar[0];
$format_p2ptm =1;
$tanggal = date("Y-m-d H:i:s");
$jenis ='p2ptm';
$nama_file2 = $gambar[1];
$format_p2ptm2 =2;
$jenis ='p2ptm';
$nama_file3 = $gambar[2];
$format_p2ptm3 =3;
$jenis ='p2ptm';
$nama_file4 = $gambar[3];
$format_p2ptm4 =4;
$jenis ='p2ptm';
$nama_file5 = $gambar[4];
$format_p2ptm5 =5;
$jenis ='p2ptm';
if (!$nama_file1 == "") {
$this->app_model->insert_data('penyakit', $format_p2ptm, $tanggal,$nama_file1,$jenis, $this->session->userdata('id_puskesmas'));
}
if(!$nama_file2 == ""){
$this->app_model->insert_data('penyakit', $format_p2ptm2, $tanggal,$nama_file2,$jenis,$this->session->userdata('id_puskesmas'));
}
if(!$nama_file3 == ""){
$this->app_model->insert_data('penyakit', $format_p2ptm3, $tanggal,$nama_file3,$jenis,$this->session->userdata('id_puskesmas'));
}
if(!$nama_file4 == ""){
$this->app_model->insert_data('penyakit', $format_p2ptm4, $tanggal,$nama_file4,$jenis,$this->session->userdata('id_puskesmas'));
}
if(!$nama_file5 == ""){
$this->app_model->insert_data('penyakit', $format_p2ptm5, $tanggal,$nama_file5,$jenis,$this->session->userdata('id_puskesmas'));
}
redirect('puskesmas/view_puskesmas');
}
Here is my view form:
<?php echo form_open_multipart('puskesmas/proses_upload');?>
<br><br><br>
<p>
<h4>1. Form Lap PTM</h4>
</p>
<p>
<label>FORMAT : </label>
<input type="text" name="format" style="width:230px; height:25px; border:2; "
placeholder="Form Lap PTM" readonly>
</p>
<label >Input file : </label>
<input type="file" name="userfile[]">
<br><br><br>
<p>
<h4>2. Form Lap Posbindu</h4>
</p>
<p>
<label>FORMAT : </label>
<input type="text" name="format2" style="width:230px; height:25px; border:2; "
placeholder="Form Lap Posbindu " readonly =>
</p>
<label>Input file : </label>
<input type="file" name="userfile[]">
<br><br><br>
<p>
<h4>3. Form Lap IVA</h4>
</p>
<p>
<label>FORMAT : </label>
<input type="text" name="format3" style="width:230px; height:25px; border:2; "
placeholder="Form Lap IVA " readonly>
</p>
<label>Input file : </label>
<input type="file" name="userfile[]">
<br><br><br>
<p>
<h4>4. Form Lap Jiwa</h4>
</p>
<p>
<label>FORMAT : </label>
<input type="text" name="format4" style="width:230px; height:25px; border:2; "
placeholder="Form Lap Jiwa" readonly>
</p>
<label>Input file : </label>
<input type="file" name="userfile[]">
<br><br><br>
<p>
<h5>5. Form Lap Indera dan Gimul</h5>
</p>
<p>
<label>FORMAT : </label>
<input type="text" name="format5" style="width:230px; height:25px; border:2; "
placeholder="Form Lap Indera_dan Gimul" readonly>
</p>
<label>Input file : </label>
<input type="file" name="userfile[]">
<br><br><br>
<!-- <button class="" type="submit">Upload</button> -->
<input class="btn btn-primary btn-lg" type="submit" value="Upload">
<?php echo form_close() ?>
if I upload files one of the 5 inputs that for the column I fill in the input is true, but why do the remaining 4 other inputs also enter data? even though I only input 1 input
its my phpmyadmin foto input
There will always be 5 files sent due to 5 inputs being sent, you need to verify whether or not they are empty or filled.
$jumlah = count(array_filter($_FILES['userfile']['name']));
Will give you the true count value of how many files have been uploaded, removing the blanks. You would ther need to update the rest of your code to accommodate for there only being x amount of array values.
To keep your current code with minimal changes you can add an if statement to check if the name is blank
$file_name = $_FILES['userfile']['name'][$i];
$gambar[$i] = ''; //set it to blank by default for your checks further down
if($file_name != ''){
$explode = explode('.',$file_name);
// die(print_r($explode));
$ekstensi = pathinfo($file_name, PATHINFO_EXTENSION);
$bulan = date('M');
$tahun = date('y') ;
$kode = $this->session->userdata('kode_puskesmas');
$new_file_name = $explode[1].'_'.$kode.'_'.$bulan.$tahun.'.'.$ekstensi;
$tmp_name = $_FILES['userfile']['tmp_name'][$i];
move_uploaded_file($tmp_name, "file/".$new_file_name);
$gambar[$i] = $new_file_name;
}
Related
i make alert if image size > to max_size with javascript. but why always appeared alert "too big picture" when the image size under max_size. there was something I forgot?
my view :
<?php echo form_open_multipart('#', array('id' => 'form-produk'));?>
<input type="text" name="nama" class="form-control" id="nama" placeholder="Nama produk" required>
<input type="file" name="gambar" class="form-control" id="gambar" required>
<input type="file" name="gambar_tambah" class="form-control" id="gambar" required>
<button type="submit" style="background-color:#1c2d3f;" class="simpan_produk btn btn-primary">Simpan Produk</button>
<?php echo form_close();?>
my javascript :
$('.simpan_produk').click(function(){
var UrlToPass = $("#form-produk").serialize();
$.ajax({ // Send the credential values to another checker.php using Ajax in POST menthod
type : "POST",
data : UrlToPass,
url : baseURL + "trueaccon2194/produk/proses_tambah_produk",
beforeSend: function(){
$('.simpan_produk').prop('disabled', true);
$('.simpan_produk').html('sedang menyimpan...'); //Loading button text
},
success : function(write){
$('.simpan_produk').prop('disabled', false);
$('.simpan_produk').html('Simpan'); //reset button text to original text
if (write=="datasuccesswrite"){
alert('data is saving!');
window.location.href = "../";
}else if(write=="imageistoolarge"){
alert('too big picture!');
location.reload();
}
}
});
return false;
});
my controller :
function proses_tambah_produk(){
$config['upload_path'] = 'assets/img/produk';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = 300;
$config['overwrite'] = TRUE;
//$config['maintain_ratio'] = TRUE;
//$config['create_thumb'] = TRUE;
//$config['max_width'] = 75;
//$config['max_height'] = 50;
$this->load->library('upload', $config);
if($_FILES['gambar']['size'] > 300){
log_helper("produk", "Gagal Menambah produk baru");
echo"imageistoolarge";
//redirect('trueaccon2194/produk/tambah_produk');
}else if(!$this->upload->do_upload('gambar_tambah')){
log_helper("produk", "Gagal Menambah produk baru");
echo"imageistoolarge";
//redirect('trueaccon2194/produk/tambah_produk');
}else{
$result=array();
$files = $_FILES;
$count = count($_FILES['gambar_tambah']['name']);
for($i=0; $i<$count; $i++)
{
$_FILES['gambar_tambah']['name']= $files['gambar_tambah']['name'][$i];
$_FILES['gambar_tambah']['type']= $files['gambar_tambah']['type'][$i];
$_FILES['gambar_tambah']['tmp_name']= $files['gambar_tambah']['tmp_name'][$i];
$_FILES['gambar_tambah']['error']= $files['gambar_tambah']['error'][$i];
$_FILES['gambar_tambah']['size']= $files['gambar_tambah']['size'][$i];
$this->upload->do_upload('gambar_tambah');
$upload_data = $this->upload->data();
$name_array[] = $upload_data['file_name'];
$fileName = $upload_data['file_name'];
$images[] = $fileName;
$result[] = $files['gambar_tambah']['name'][$i];
}
$target = $this->input->post('nama');
$this->upload->do_upload('gambar');
$gambar = $_FILES['gambar']['name'];
$data = $this->input->post();
$data['id'] = $this->data['id'];
$this->produk_adm->add($data, $gambar, $result);
log_helper("produk", "Menambah Produk ".$target."");
echo "datasuccesswrite";
//redirect('trueaccon2194/produk');
}
}
In your view id must be unique for each element.So make id of second file id="gambar_tambah".
<?php echo form_open_multipart('#', array('id' => 'form-produk'));?>
<input type="text" name="nama" class="form-control" id="nama" placeholder="Nama produk" required>
<input type="file" name="gambar" class="form-control" id="gambar" required>
<input type="file" name="gambar_tambah" class="form-control" id="gambar_tambah" required>
<button type="submit" style="background-color:#1c2d3f;" class="simpan_produk btn btn-primary">Simpan Produk</button>
<?php echo form_close();?>
id should be unique for any element.
<input type="file" name="gambar" class="form-control" id="gambar" required>
<input type="file" name="gambar_tambah" class="form-control" id="gambar_tambah" required>
You are not checking size of both files uploaded.
if($_FILES['gambar']['size'] > 300){
log_helper("produk", "Gagal Menambah produk baru");
echo"imageistoolarge";
} else {
// upload files
}
if($_FILES['gambar_tambah']['size'] > 300){
log_helper("produk", "Gagal Menambah produk baru");
echo"imageistoolarge";
} else {
// upload files
}
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;
}
I have a following code where I can upload a single image. This image gets stored in both database and folder. Now what I want is to add multiple images. How can I do that. Help me to come out of this.
<?php
$uploadDir ="C:/wamp/www/dragongym/customers/";
if(isset($_POST['submit']))
{
$intime =DATE("H:i", STRTOTIME($_POST['intime']));
$outtime =DATE("H:i", STRTOTIME($_POST['outtime']));
date_default_timezone_set('Asia/Calcutta');
$today = date("Y-m-d");
$msg="";
$res = "SELECT customer_id FROM customer ORDER by customer_id DESC LIMIT 1";
$qur = mysql_query($res);
while($row = mysql_fetch_array($qur, MYSQL_BOTH))
{
$last_id = $row['customer_id'];
$plus_id = 1;
}
if( $last_id !="")
{
$cust_id = $last_id + $plus_id;
}
$filePath="";
if($_FILES['cimage']['size'] > 0)
{
// echo $cust_id;
// Temporary file name stored on the server for pdf
$filename = basename($_FILES['cimage']['name']);
$extension = pathinfo($filename, PATHINFO_EXTENSION);
$new = $cust_id.'.'.$extension;
$tmpName1 = $_FILES['cimage']['tmp_name'];
$fileSize = $_FILES['cimage']['size'];
$fileType = $_FILES['cimage']['type'];
$filePath = $uploadDir . $new;
$resultes = move_uploaded_file($tmpName1, $filePath);
if (!$resultes)
{
echo "Error uploading file";
exit;
}
if(!get_magic_quotes_gpc())
{
$new = addslashes($new);
$filePath = addslashes($filePath);
}
}
$sql = 'INSERT INTO customer(customer_name,roll_no,customer_number,customer_address,tariff_id,intime,outtime,customer_image,active,joining_date) VALUES("'.$_POST['name'].'","'.$_POST['roll'].'","'.$_POST['number'].'","'.$_POST['address'].'","'.$_POST['tariff'].'","'.$intime.'","'.$outtime.'","'.$filePath.'","1","'.$today.'")';
$msg="<p style=\"color:#99CC00; font-size:13px;\"> Successfully!</p>";
if (!mysql_query($sql, $link))
{
die('Error: ' . mysql_error());
}
}
?>
<form action="#" method="post" enctype="multipart/form-data">
<h2>Registration Form</h2><?php echo $msg; ?>
<label>Name</label>
<input type="text" value="" name="name" id="name" required class="txtfield">
<label>Roll Number</label>
<input type="text" value="" name="roll" id="roll" required class="txtfield">
<label>Mobile Number</label>
<input type="text" value="" name="number" required class="txtfield" id="mobnum">
<label>Address</label>
<textarea name="address" class="txtfield"></textarea>
<label>Upload Photo</label>
<input type="file" value="" name="cimage" class="txtfield">
<!-- <label style="display: block">Timing</label>
<input type="text" value="" name="intime" placeholder="Intime" required class="timefield timepicker">
<input type="text" value="" name="outtime" placeholder="Outtime" required class="timefield timepicker">-->
<input type="submit" value="Save" name="submit" class="btn buttonside1">
</form>
You can use jquery plugin for that..
there is lots of plugin available on google..try this one http://blueimp.github.io/jQuery-File-Upload/
to do multiple file upload you should first have multiple="true" in your tab like so
<input type="file" name='files[]' multiple='true'/>
then use foreach loop to upload files.
I am planning to have a web gallery. However, it is hard for me to use PHP to insert DB. The following code:
HTML -- I want to make a form which has category and multiple images that can be inserted into DB at the same time.
<form action="upload" method="POST" enctype="multipart/form-data">
<p> Upload : <input type="file" id="file" name="images" /> </p>
<p> Category : <input type="text" name="imageCategory"> </p>
<p> <input type="submit" name="submit" value="Upload!" /> </p>
</form>
DATABASE
I am using imageName as VARCHAR not BLOB TYPE.
PHP
<?php
include ("dbConnect.php");
if(isset($_POST["submit"])) {
$image = $_POST['images']['tmp_name'];
$imageName = $_POST['images']['name'];
$imageSize = $_POST['images']['size'];
$imageType = $_POST['images']['type'];
$imageCategory = $_POST['imageCategory'];
$result = $mysqli->query("INSERT INTO imageTable (imageName, imageCategory, imageSize, imageType)
VALUES ('$imageName', '$imageCategory', '$imageSize' , '$imageType' );")
or die(mysqli_error($mysqli));
} else {
echo "<p> It is not working </p>";
}
header("location: index");
$mysqli->close();
?>
The problem is, the category is the only one has inserted into the database successfully. But not with the imageName, imageType, and imageSize. And also i want the image to be stored into database so that I can retrieve the image from DB on the other web page. Any ideas?
You can use 'multiple' property in the 'input' tag like this :
<form action="file-upload.php" method="post" enctype="multipart/form-data">
Send these files:<br />
<p> <input name="userfile[]" type="file" multiple='multiple' /> </p>
<p> Category : <input type="text" name="imageCategory"> </p>
<input type="submit" value="Send files" />
</form>
User can select multiple files and upload them.
And at the server you will do this :
if (isset($_FILES["userfile"]) && !empty($_FILES["userfile"])) {
$image = $_FILES['userfile']['tmp_name'];
$imageName = $_FILES['userfile']['name'];
$imageSize = $_FILES['userfile']['size'];
$imageType = $_FILES['userfile']['type'];
$imageCategory = $_POST['imageCategory'];
$len = count($image);
$path = "images/";
for ($i = 0; $i < $len; $i++) {
if (isset($imageName[$i]) && $imageName[$i] !== NULL) {
if(move_uploaded_file($image[$i], $path.$imageName[$i])) {
$result = $mysqli->query("INSERT INTO imageTable (imageName, imageCategory, imageSize, imageType) VALUES ('$imageName[$i]', '$imageCategory', '$imageSize[$i]' , '$imageType[$i]' )");
}
}
}
}
$mysqli->close();
header("location: index");
First off the file information won't be in the $_POST global variable it will be in the $_FILES
From http://php.net/manual/en/features.file-upload.multiple.php (modified):
Form
<form action="file-upload.php" method="post" enctype="multipart/form-data">
Send these files:<br />
<p> <input name="userfile[]" type="file" /> </p>
<p> <input name="userfile[]" type="file" /> </p>
<p> Category : <input type="text" name="imageCategory"> </p>
<input type="submit" value="Send files" />
</form>
Parse the global file variable to an array (we'll assume this is a helper function called parseFiles.php):
<?php
function reArrayFiles(&$file_post) {
$file_ary = array();
$file_count = count($file_post['name']);
$file_keys = array_keys($file_post);
for ($i=0; $i<$file_count; $i++) {
foreach ($file_keys as $key) {
$file_ary[$i][$key] = $file_post[$key][$i];
}
}
return $file_ary;
}
Move the files and insert the file information into the database :
<?php
include ("dbConnect.php");
include ("parseFiles.php");
if ($_FILES['upload']) {
$file_ary = reArrayFiles($_FILES['userfile']);
foreach ($file_ary as $file) {
$image = $file['tmp_name'];
$imageName = $file['name'];
$imageSize = $file['size'];
$imageType = $file['type'];
$imageCategory = $_POST['imageCategory'];
$result = $mysqli->query("INSERT INTO imageTable (imageName, imageCategory, imageSize, imageType)
VALUES ('$imageName', '$imageCategory', '$imageSize' , '$imageType' );")
or die(mysqli_error($mysqli));
}
} else {
echo "<p> It is not working </p>";
}
header("location: index");
$mysqli->close();
Hopefully that should do the job. I've not tested this I've just blind coded it so there may be some bugs
Now i am trying to upload pdf files by the form in php but when i upload it, it doesn't work because function is_uploaded_file($_FILES['file']['tmp_name']) return false
How can i solve it please ??
I am using localhost and I verify php.ini
; Whether to allow HTTP file uploads.
; http://php.net/file-uploads
file_uploads = On
; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
; http://php.net/upload-tmp-dir
upload_tmp_dir = "E:/wamp/tmp"
; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize = 50M
This is my code :
Class
<?php
require '../includes/master.inc.php';
// Kick-out users who aren't logged in as an admin
$auth->requireAdmin('../login_admin_cp.php');
if (isset($_POST['btnsubmit'])) {
$error->blank($_POST['field_name'], "field_name");
if ($error->ok()) {
$field_name = $_POST['field_name'];
if (is_uploaded_file($_FILES['file']['tmp_name'])) {
// exit;
$mypath = "upload/";
$file_name = $_FILES['file']['name'];
$tmp = $_FILES['file']['tmp_name'];
$n = $mypath . $file_name;
$t = $_FILES['file']['size'];
$filesize = round($t / 1024) . " KB";
$extintion = strchr($n, ".");
$extintion = strtolower($extintion);
$file_extintion_allow = array(".pdf");
if ($n == "") {
echo "Error in upload";
} elseif (!in_array($extintion, $file_extintion_allow)) {
echo "من فضلك ادخل المادة بطريقة صحيحة";
exit;
} else {
//rename the file when upload by query name
$quaryname = rand(11111, 99999);
$rename = "../" . $mypath . $quaryname . $extintion;
$URL = substr($rename, 10, strlen($rename));
move_uploaded_file($tmp, $rename);
if (!empty($_POST['hide'])) {
$hide = $_POST['hide'];
$db->query("insert into field (field_name,url,hide)values('$field_name','$URL','$hide')");
$sess->set_flashdata('success_msg', 'تم حفظ المادة بنجاح');
redirect("field_new.php");
} else {
$db->query("insert into field (field_name,url,hide)values('$field_name','$URL','0')");
$sess->set_flashdata('success_msg', 'تم حفظ المادة بنجاح');
redirect("field_new.php");
}
}
} else {
exit;
}
} else {
$sess->set_flashdata('error_msg', 'حقل المادة فارغ');
}
}
// View
include('views/header.php');
include('views/field_form_new.php');
include('views/footer.php');
?>
Form
<!-- Form elements -->
<div class="grid_9">
<div class="module">
<h2><span> رفع المادة</span></h2>
<div class="module-body">
<?php echo $error;?>
<form action="field_new.php" method="post" name="myform" enctype="multipart/form-data">
<p>
<label> اسم المادة</label>
<input type="text" class="input-medium" name="field_name" value="" />
</p>
<p style="font-size:14px"> رفع المادة </p>
<p>
<label> الرابط</label>
<input type="text" class="input-medium" name="link" value="<?php $link; ?>" />
</p>
أو<br/> <br/>
<p>
<input type="hidden" class="input-medium" name="MAX_FILE_SIZE" value="1000000">
<label>رفع الملف </label>
<input type="file" class="input-medium" name="file" />
</p>
<p>
<input type="checkbox" name="hide" value="1" />تفعيل المادة
</p>
<fieldset>
<input class="submit-green" type="submit" name="btnsubmit" value="اضافة" />
</fieldset>
</form>
</div> <!-- End .module-body -->
</div> <!-- End .module -->
<div style="clear:both;"></div>
</div> <!-- End .grid_6 -->
Maybe the file is to big or the destination directory is not writeable for php/apache.
Check the folder permission "E:/wamp/tmp" and try to upload a smaller file.
If a smaller file works, check these values in the php.ini file:
post_max_size
max_input_time
memory_limit