which i need to update the profil user with or without user's photo profile, here my code in view
<input type="file" name="file_foto" class="form-control">
how i can read the file is empty or not, to chose when update data is with image or without image.
and how to make unlink image to delete previouse image in Codeigniter. thanks
here is my Controller
if (empty($_FILES['file_foto']['name'])) {
$data_profil = array(
'name'=>$name
);
}else{
$foto_up = $this->do_upload_image('file_foto');
$data_profil = array(
'name'=>$name,
'foto'=>$foto_up['file_name']
);
}
if($_FILES['file_foto']['name'])){
code to upload image
}
oh sory all, my problem now solve, just use this code
if (empty($_FILES['file_foto']['name']))
Related
I run into this problem sometimes with the update form which contains files to upload to a database.
now to explain :I have a submit form with two input Fields. The first input is the name of the image , and the second one is the image itself. The form is submitted to upload.php as you can see the code
upload.php
if(isset($_POST['submit'])){
include 'connection.php';
$imageName=$_POST['imageName'];
$image=$_FILES['image'];
$imageTmpName=$image['tmp_name'];
$imageName=$image['name'];
$imageExt=explode('.',$imageName);
$imageExt=strtolower(end($imageExt));
$newImageName=uniqid('',true).'.'.$imageExt;
$path='C:/xampp/htdocs/test/img/'.$newImageName;
if(move_uploaded_file($imageTmpName,$path)){
echo 'file moved';
}
$query="INSERT INTO imagedata(imageName,image) VALUES ('$imageName','$newImageName')";
if(mysqli_query($conn,$query)){
echo 'data posted to database';
}
}
You can see from the code that I am moving the image temporary name to my path and in the same time I'm and storing the new image name into the database.
I am using the same principle update the image name and the image itself in a new form that is submit to update.php as show below:
if(isset($_POST['update'])){
include 'connection.php';
$uImageName=$_POST['uimageName'];
$uImage=$_FILES['uImage'];
$uImageTmpName=$Image['tmp_name'];
$uImageName=$uImage['name'];
$uImageExt=explode('.',$uImageName);
$uImageExt=strtolower(end($uImageExt));
$uNewImageName=uniqid('',true).'.'.$uImageExt;
$uPath='C:/xampp/htdocs/test/img/'.$uNewImageName;
if(move_uploaded_file($uImageTmpName,$uPath)){
echo 'file updatet';
}
$query="UPDATE imagedata SET imageName='$uImageName',image='$uNewImageName'";
if(mysqli_query($conn,$query)){
echo 'data updatet to database';
}
}
Remember that I am querying database and echo the values inside the input field values itself.Now the problem occurs in the update.php file, whenever i update the image name without uploading any new image, it will update the database with a blank image and return that to my update form.
So what i want to do is whenever i want to update the database without an image from my update form, i want the previous uploaded image to remain the same. Or if i want a new image to be uploaded to the database then i want the database to be updated with the new image and also to delete the old image that i have previous uploaded.
I want to thank you for your help with this
Simple way to keep old image if new image not selected!
Here is the easy way, create a hidden input and echo your old image name in value, and keep new image value empty as follows:
<input type="hidden" name="YourOldFile" value="YourOldFileName">
<input type="file" name="YourNewFile" value="">
Then do if statement in PHP part:
if(!empty($POST['YourNewFile'])) {
$newFile = $_POST['YourNewFile'];
// Then do your update query with uploading new image here and delete old image from the server.
} else {
$YourOldFileName = $_POST['YourOldFile'];
//Then do your query here with oldname and do not delete image from server.
}
Note: you can do same for insert and update, and you need to set where clause for update query.
UPDATE :
Displaying with or without image extention is a different question because its a part of your upload code, and how you save and display.
So, your codes in page looks like this :
include 'connection.php';
if(!empty($_FILES['uImageName'])) {
// Then do your update query with uploading new image here and delete old image from the server.
if(isset($_POST['update'])){
$uImageName=$_FILES['uimageName'];
//Your upload code doesnt look right coded to me, I would use an upload class, You can search for how to upload image
// I use verot upload class its very simple.
$uImageTmpName=$Image['tmp_name'];
$uImageName=$uImage['name'];
$uImageExt=explode('.',$uImageName);
$uImageExt=strtolower(end($uImageExt));
$uNewImageName=uniqid('',true).'.'.$uImageExt;
$uPath='C:/xampp/htdocs/test/img/'.$uNewImageName;
if(move_uploaded_file($uImageTmpName,$uPath)){
echo 'file updated';
}
$query="UPDATE imagedata SET imageName='$uImageName'";
if(mysqli_query($conn,$query)){
echo 'data updatet to database';
}
}
} else {
$YourOldFileName = $_POST['YourOldFile'];
//Then do your query here with oldname and do not delete image from server.
//Solution 1. You can update image column with your old image name.
$query="UPDATE imagedata SET image='$YourOldFileName'";
//Solution 2. You can update all other columns without image column dont update image column and dont include image column in your query.
$query="UPDATE imagedata SET yourColumnName='$DataYouWantToInsert'";
if(mysqli_query($conn,$query)){
echo 'data updatet to database';
}
}
<form>
/*Your inputs here*/
<input type="hidden" name="YourOldFile" value="YourOldFileName">
<input type="file" name="YourNewFile" value="">
/*Your buttons*/
</form>
Read comments in code.
Hi i'm new using Laravel and i have a form with a upload file button, but this is optional on my form, if i don't want to upload an image, my app stores one by default (no-image.jpg) and this image is in my storage_path, how i can get this path and assign it to my new record?
I tried this but it doesn't work:
if($request->infopath == null){
$request->infopath = public_path() . "/infopath"."/". 'no-image.jpg';
PreguntaAbierta::create($request->all());
Session::flash('store-success','Datos agregados correctamente!');
return redirect()->route('abierta.index');
}
'SOLUTION'
I finally found a solution , although an unorthodox but it does what I want, but I doubt remains =/
On my view:
#if($pregunta->infopath == null)
<td><img src="infopath/no-image.jpg" alt="" style="width:100px"/></td>
#endif
LOL
You can use ->with
return redirect()->route('abierta.index')->with('infopath',$request->infopath);
and get this as $infopath in abierta.index
Also there is multiple option you can check here : https://laravel.com/docs/5.1/responses
You can do like this:
$input = $request->all();
$input['infopath'] = $your_image_public_path;
PreguntaAbierta::create($input);
I am making an edit form where the user can edit item details and change the item image. The item details get updated in the database fine but the item image does now change. Below is my code...
if($_POST['item_img']){
$old_img = "img/items/$item_id.jpg";
$default_img = "img/items/default.jpg";
if (file_exists($old_img)) {
unlink($old_img);
$filename = $item_id.'.jpg';
move_uploaded_file ($_FILES["item_img"]["tmp_name"],"img/items/".$filename)
}elseif(file_exists($default_img)) {
unlink($default_img);
$filename = $item_id.'.jpg';
move_uploaded_file ($_FILES["item_img"]["tmp_name"], "img/items/".$filename);
}
}
I think it is my if($_POST['item_img']) statement that is causing this issue. What could I change the loop to.
The first nested if statement checks if there is an image attached, and the second if statement checks if a default image is attached. In both cases it deletes the old image and sets a new image with the "item_id" as its name.
Below is the code for my for upload field:
<form class="stock" method="post" enctype="multipart/form-data" action="">'
<input type="file" name="item_img">
</form>
Change it
$_POST['item_img']
To
$_FILES["item_img"]["tmp_name"]
I am trying to create a cross platform application and I would like to save images from different devices in the database using Codeigiter. I am able to select the images form the device but I am confused and stocked on how to save images and retrieving them from the database.
Controller
function addstatesoothing() {
// write user data into the user database
$config['upload_path'] = './';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000000';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$config['encrypt_name'] = FALSE;
$this->load->library('upload', $config);
$image_path = $this->upload->data('soothing-img');
$data = array(
'soothing-img' => $image_path[full_path],
'soothing-title' => $this->input->post('soothing-title'),
);
$this->favourite_db->addstatesoothing($data);
$this->load->view('soothing');
}
Model
function addstatesoothing($data) {
$this->load->database();
$this->db->insert('soothing', $data);
return $data;
}
View
<form method="post" action="addstatesoothing">
<!-- <button data-theme="d" onclick="capturePhoto();">Capture Photo</button> -->
<a data-theme="d" onclick="getPhoto(pictureSource.PHOTOLIBRARY);">Click to Add Pictures</a>
<img style="width:100%;" id="largeImage" src="" name="soothing-img"/> <br>
<input type="text" name="soothing-title" id="Climbed a Mountain" value="" placeholder="Climbed a Mountain"/>
<button data-theme="a">Save Memory</button>
</form>
To save image in database:
1 - Your image field on database should be BLOB type.
2 - Get file content from your image:
$image = file_get_contents($_FILES['image']['tmp_name']);
3 - Save it in your BLOB type field on database
mysql_query("insert into images (image) values ('$image') ");
I don't know if you use another approach to save data in database, your code is a bit different , but that's the way, right? In the end it'll be a row inserted in database, If you have any doubt, comment here, but at this point is all easy, ham? now let's go to retriving images
First of all, storing images content in database is a real mistake, but that isn't the question right? So I'm not an expert in this approach, but is how I would:
Create an another page like image.php, receiving some id or image name by parameter
image.php:
<?php
$id = $_GET['id'];
$query = "SELECT image FROM images WHERE `id`=$id";
$result = mysql_query($query);
header("Content-type: image/jpeg"); //if is another format like .png change here
while($row = mysql_fetch_assoc($result))
{
echo $row['image'];
}
?>
Nice, you have a nice *.php file that response an image just like any *.jpg file, so now call it in your HTML
SomePage.html:
<img src="image.php?id=12345" />
Ok, all done,
but why I told that saving image content is bad?: SO fellas are awesome, we have amazing material to explain why saving images in database is BAD! You could check it, here's:
Storing Images in DB - Yea or Nay?
Store images in database or on file system
The first thing in this question is your form is not able to upload image because your not using multipart form data
you can try like this
<form enctype="multipart/form-data"></form>
I have recently started working on zend framework. I want to upload a profile picture and rename & re-size it. Am using the code below. with this am able to upload but am not able to rename and am not getting a way to re-size the uploaded file.
if($this->getRequest()->isPost())
{
if(!$objProfilePictureForm->isValid($_POST))
{
//return $this->render('add');
}
if(!$objProfilePictureForm->profile_pic->receive())
{
$this->view->message = '<div class="popup-warning">Errors Receiving File.</div>';
}
if($objProfilePictureForm->profile_pic->isUploaded())
{
$values = $objProfilePictureForm->getValues();
$source = $objProfilePictureForm->profile_pic->getFileName();
//to re-name the image, all you need to do is save it with a new name, instead of the name they uploaded it with. Normally, I use the primary key of the database row where I'm storing the name of the image. For example, if it's an image of Person 1, I call it 1.jpg. The important thing is that you make sure the image name will be unique in whatever directory you save it to.
$new_image_name = 'new';
//save image to database and filesystem here
$image_saved = move_uploaded_file($source, '../uploads/thumb'.$new_image_name);
if($image_saved)
{
$this->view->image = '<img src="../uploads/'.$new_image_name.'" />';
$objProfilePictureForm->reset();//only do this if it saved ok and you want to re-display the fresh empty form
}
}
}
To Rename a file while uploading, you will have to add the "Rename-Filter" to your file-form-element. The class is called Zend_Filter_File_Rename.
// Create the form
$form = new Zend_Form();
// Create an configure the file-element
$file = new Zend_Form_Element_File('file');
$file->setDestination('my/prefered/path/to/the/file') // This is the path where you want to store the uploaded files.
$file->addFilter('Rename', array('target' => 'my_new_filename.jpg')); // This is for the filename
$form->addElement($file);
// Submit-Button
$form->addElement(new Zend_Form_Element_Submit('save');
// Process postdata
if($this->_request->isPost())
{
// Get the file and store it within the specified destination with the specified name.
$file->receive();
}
To make the filename dynamically you may name it with a timestamp or something. You may also apply the Rename-filter within your post-data-processing before the call of $file->receive(). This could be useful if you insert a row into a table and want to name the file with the id of the just inserted row.
Since you want to store a profile picture you could get the id of the user from your db and name the pic with that id.