unable to make image upload optional in codeigniter - php

I have four images to upload. And all these four images are optional in my form.
I tried to make it optional but I could not solve my problem. Please check my code below and let me know any solution for it.

<?php
if(isset($_FILES))
{
if(empty($_FILES['testfile']['name']))
{
echo 'no';
}else{
// do the form validation here
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST" enctype="multipart/form-data">
<input type="file" name="testfile"/>
<input type="submit" value="upload">
</form>
here is some test you can try i hope it helps

Related

$_FILES[""][""] is empty in uploading files

I really Dont know What should I do the problem is that $_Files is always empty when I try to upload a file.
<form method="get" action="pic.php" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="submit">
</form>
<?php
$name=$_FILES["file"]["name"];
$tmp_name=$_FILES["file"]["tmp_name"];
echo "php is talking";
echo $name;
if(isset($name)&&!empty($name))
{
echo "OK";
$location="uploads/";
if(move_uploaded_file($name,$location.$name))
{
echo "the file has been Uploaded";
}
}
?>
You have to change the HTTP method in the form from GET to POST
<form method="POST" action="pic.php" enctype="multipart/form-data">

Submiting a form using PHP

I am trying to submit a form using PHP. I am trying to grab the value from two file inputs in my form, yet when I try to index them with my PHP code, I keep getting an error.
The error I get:
Undefined index: profile-pic in C:\xampp\htdocs\shareitme\form-test.php on line 5
Undefined index: cover-pic in C:\xampp\htdocs\shareitme\form-test.php on line 6
My Code:
<?php
if (isset($_POST['submit'])) {
$profile_pic= time() . $_FILES['profile-pic']['name'];
$cover_pic= time() . $_FILES['cover-pic']['name'];
}
?>
<form id="editprofile" method="post" action="<?php echo $_SERVER['PHP_SELF']
?>">
<input type="hidden" name="MAX_FILE_SIZE" value="5000000"/>
<input type="file" name="profile-pic"/>
<input type="file" name="cover-pic"/>
<input type="submit" name="submit" value="submit"/>
</form>
I know I have the names right, what am I doing wrong?
For uploading a file include enctype="multipart/form-data" in your form, like below:
<form id="editprofile" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']
?>">
The enctype attribute specifies how the form-data should be encoded when submitting it to the server.
For more info on forms : http://www.w3schools.com/tags/att_form_enctype.asp
For renaming a uploaded file, it better to upload it to server with move_uploaded_file and than rename it.
try:
<?php
if (isset($_REQUEST['go']) && $_REQUEST['go'] == "1") {
$profile_pic= time() . $_FILES['profile-pic']['name'];
$cover_pic= time() . $_FILES['cover-pic']['name'];
}
?>
<form id="editprofile" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="MAX_FILE_SIZE" value="5000000"/>
<input type="file" name="profile-pic"/>
<input type="file" name="cover-pic"/>
<input type=hidden name="go" value="1">
<input type="submit" name="submit" value="submit"/>
</form>
Whenever you are want to upload file. you need to add enctype in from.
`enctype="multipart/form-data"'
This should work
<form id="editprofile" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']
?>">
What you're trying to do here, if I'm not mistaken, is upload images on your server, right? You can simply use a third-party upload tool for that. I'd recommend AjaxUpload as it is based on AJAX and pretty easy to implement.
For simplicity, I'm applying the upload functionality on one image. You can simply put it inside a function and reuse it.
<p id="showMsg"></p>
<input type="file" name="profile-pic" id="profilePic" />
JQuery:
$("#profilePic").ajaxUpload({
url : $_SERVER['PHP_SELF'],
name: "file",
onSubmit: function() {
$('#showMsg').html('Uploading ... ');
},
onComplete: function(result) {
$('#showMsg').html('File uploaded with result' + result);
}
});
PHP:
<?php
if (isset($_FILES))
{
$file = $_FILES['file'];
move_uploaded_file($_FILES["file"]["tmp_name"], 'upload_dir/' . $_FILES["file"]["name"]); // This function will upload the image to 'upload_dir' folder. You can modify it as per your requirements.
}
?>
1) if you are using file type then your form attribute should be enctype="multipart/form-data".
<form id="editprofile" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
2) Before using your post value it is best practice to check whether its setted or not.
$pic=isset($_FILES['profile-pic']['name']) ? $_FILES['profile-pic']['name'] : '';
Use enctype="multipart/form-data" in form tag.
Whenever you want to post binary datas you must have to set this enctype attribute.

how do I move my uploaded file to different folder

Hi Hi trying to upload a file via a form move it to another folder and then print its name. But doesnt work don't know why.
<form method="post" action='exercice.php' id="form1">
<input type="file" name="files" id="files" onChange="submitForm();">
</form>
<?php
if (isset($_FILES['files']))
{
move_uploaded_file($_FILES['files']['tmp_name'], "uploaded/");
echo $_FILES['files']['name'];
}
?>
form missing:
enctype="multipart/form-data"
ref:
spec
<form method="post" action='exercice.php' id="form1">
<input type="file" name="files" id="files" onChange="submitForm();">
</form>
<?php
if (isset($_FILES['files']))
{
move_uploaded_file($_FILES['files']['tmp_name'], "uploaded/".$_FILES['files']['name']);
echo $_FILES['files']['name'];
}
?>
You need to include the file name in the upload path, like shown above.
Also, where is your upload (ajax) code?
Did isset($_FILES['files']) return true?

<input type=file> not getting image

I am trying to get image file name by tag, But when I check it through isset($_FILES['imgFile']), it returns always false.
Here is my HTML tag for getting image file:
<input type="file" name="imgFile" accept="image/*" id="imgFile" src=""/>
Here is my php code to retrieve it:
if(isset($_FILES['imgFile']))
{
$img = $_FILES['imgFile']['name'];
echo $img";
}
else
{
echo "Image not set";
}
It always generate "Image not set" as an output though I have selected an image.
Are you using the correct enctype on the form?
<form enctype="multipart/form-data">
This is required when using a file upload element.
just use:
enctype="multipart/form-data"
Say this is your form:
<form action="same_page.php" method="post" enctype="multipart/form-data">
<input type="file" name="imgFile" accept="image/*" id="imgFile" src=""/>
<input type="submit" name="upload" value="Upload" />
</form>
and your php code to retrive name of the image is in the same page where the form is, then your code should be like this:
<?php
if (isset($_POST["upload"])) {
if (isset($_FILES['imgFile'])) {
$img = $_FILES['imgFile']['name'];
echo $img;
} else {
echo "Image not set";
}
}
?>
But if your php code is in another page, then you only need to use enctype="multipart/form-data" in the form as mentioned in the other answers.

PHP Image Upload, with preview on select of a file

Ok so im making an image uploader with a chance to see what you are going to upload, before entering it into the database. My mind is kinda tired, could you please just get me through this one? thx (btw it doesnt work :p) Its on a clean page, i choose a file from my pictures on compouter, it doesn echo "hi wazzup
<?php
if(isset($_FILES['image'])){
echo "hi wazzup!";
}
?>
<form name="form" id="form" method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<input type="file" name="image" onChange="if(event.propertyName=='value') {document.getElementById("form").submit();}">
</form>
See what is the output and you will know wheater your javascript is wrong or you should use $_FILES instead of $_POST for image
<?php
if(isset($_POST))
{
if(isset($_FILES['image'])){
echo "<span style=\"z-index:1000;display:block;\">hi wazzup!</span>";
}
echo "POSTED";
}
?>

Categories