Update file via php - php

The form with the input element is below:
<?php
$post_new_file=$_FILES['post_new_file'];
if(isset($_POST['update'])){
if (!empty($post_new_file)) {
$post_file=$_FILES['post_new_file']['name'];
$post_file_temp=$_FILES['post_new_file']['tmp_name'];
move_uploaded_file($post_file_temp,"../pdf/$post_file");
}
$query="UPDATE posts SET post_file='{$post_file}' WHERE post_id='{$the_post_id}' ";
$create_post_query= mysqli_query($connect, $query);
confirmQuery($create_post_query);
?>
<form action="edit.php?source=<?php echo $the_post_id ?>" method="post" enctype="multipart/form-data">
<div class="form-group" style="border: solid #000 3px;">
<label for="post_file">Select New File</label>
<input type="file" name="post_new_file" >
</div>
</form>
I have problem when the $post_new_file exist. In this case when updating I lost the data in my db and the post can't have access into the file. In a few words I don't want changing the access of the file when I haven't insert a new file.
Thanks

you should check if $_FILES['post_new_file']['tmp_name'] is empty, the _FILES['post_new_file'] will exist even if there was an error with the file upload. you may even want to check the move upload worked as well to be extra sure you actually have the file, as the file could be uploaded but if the move fails you will lose the file as it was only stored temporarily

Related

How to store image file path by clicking image itself

Im attempting to allows users to choose from a simple set of 4 avatars that they can use as their profile picture.
I've tried using:
<input type="image" class="profile-image-icon" src="img/avatar/avatar1.png" name="image" id="image">
But I finally found that using input type="image" is only for using an image as a submit button.
The avatars in question are already in my img/avatar folder, which I then only want to store the image path in the database.
I have echoed any error messages on changeProfile.php, which is returning "Nothing getting through" so I know it is an issue with my form, but I am unsure how to proceed.
Is there anyway to allow the user to simply click on the image and it 'POSTS' to changeProfile.php where I can then retrieve the file path to store in my database?
Thanks for any help provided!
edit_profile_image.php
<form class="profile-image-form" method="POST" action="profileChanged.php">
<fieldset>
<div class="image-control">
<input type="image" class="profile-image-icon"src="img/avatar/avatar1.png" name="image" id="image">
</div>
<?php echo "<input type='text' class='form-control' id='studentNumber' name='studentNumber' value='$studentID'>"; ?>
</fieldset>
</form>
changeProfile.php
if (isset($_POST['image'])) {
$image = mysqli_real_escape_string($conn, $_POST['image']);
$studentNumber = mysqli_real_escape_string($conn, $_POST['studentNumber']);
} else {
echo "Nothing coming thorugh";
}

Retrieve Images From Folder with Codeigniter

My teacher,he was uploading and retrieving images from folder with ID recognition. It works with database.
So, when we upload an image, it will be stored in a folder with it's name changing automatically with ID.
Example: The upload form must be on "yourwebsite.com/yourpages/3144242" so the image will be stored as "3144242" and blablabla the same for other pages with ID
This is how he upload and retrieve it
<?php
$ff = './assets/images/kegiatan/'.$_SESSION['idkeg'].'.jpg';
if( file_exists( $ff ) ) {
<img src="<?= base_url()?>assets/images/kegiatan/<?=$_SESSION['idkeg']?>.jpg" />
<?php
}
?>
Retrieve images part:
if( file_exists( $ff ) ) {
<img src="<?= base_url()?>assets/images/kegiatan/<?=$_SESSION['idkeg']?>.jpg" />
<?php
}
?>
However, I change it a bit so I don't have to use a database and $_SESSION things. With this:
<?php
$ff = './assets/images/kegiatan/'.$this->input->post('img_name').'.jpg';
?>
<form id="form" class="kart" action="<?= base_url()?>gallery/upload2" method="post" enctype="multipart/form-data">
<div class="form-group">
<input type="text" class="form-control" name="img_name" required="required">
</div>
<div class="form-group">
<input type="file" class="form-control" name="foto">
</div>
<div class="form-group">
<button type="submit" class="btn btn-common" value="Update"><i class="fa fa-upload fa-2x"> Upload</i></button>
</div>
</form>
It works uploading images into folder "kegiatan" (means activity) with simply write the names of images in a form. The img_name that you have input, now become the name of that image.
Now I have a little problem to retrieve that images. How?
mistake in your form I realized is asking user to enter image name and get image from folder by using that input value.
First of all, I haven't tried upload an image with codeigniter but I will answer reference to php itself.
When we get an image as input from user by form, then we use "move_uploaded_file" function in php and we send temp_file, path with file name arguments to this function to save the file user selected in the form we prepared.
move_uploaded_file(string $filename , string $target);
// $filename: I use $_FILES['getFileUpload']['tmp_name'] for this argument
// $target: I define path with file name, eg: uploads/files/new_file_25.jpg for jpg image
So, you souldn't ask user to enter file name, because you will deifne it. Or, you must use "img_name" that you asked user to enter as file name which is most probably won't satisfy your ID prediction.
I hope this will help you.

Geting the file object from a form in Symfony

I have a scenario where I have to make use of a form the old fashioned why like this;
<form action="{{ path('admin_app_address_import') }}" method="post" name="form_import">
<div class="form-group">
<label for="inputFile">File input</label>
<input type="file" id="inputFile" accept="text/csv" name="inputFile">
<p class="help-block">Select a CSV file to import.</p>
</div>
</form>
And then get the parameters from the form like this inside my action;
public function importAction(Request $request) {
$file = $request->files->get('inputFile');
var_dump($file);exit;
//...
}
But I keep on getting null. How can I get the file I try to read from? I don't need to store it in a database whatsoever, I just need to read the content to upload that to the database. I'm making a CSV import action, but I can't seem to get the file object.
When doing var_dump($request->request->all()); I do get the filename, but that won't work right?
When allowing file uploads, the encoding type of the form must be set to multipart/form-data:
<form ... enctype="multipart/form-data">

Form Posts Text Inputs But Not File

I am trying to submit file inside a form to Laravel backend. It posts all the other text fields but not file (when I dd($request->all()))
<form class="form-horizontal" role="form" method="POST" action="" enctype="multipart/form-data">
<input id="item_price" type="item_price" class="form-control" name="item_price" value="">
<!-- other inputs -->
<input type="file" id="product_image" name="product_image" onchange="previewImage(this);"/>
<img id="previewing" src="{{asset('noImage.gif')}}" style="width: 100%; height: 100%" />
</form>
item_price seems on dd($request->all()) and also other fields are seen, but no file input
When I display the image with jQuery (previewImage) before submitting the form, it shows the photo. But after I submit the form, on Laravel side it shows all the other fields and values but not any for file (when I use dd($request->all()).
On php.ini, max-post-size is 100M.
jQuery('#previewing').click(function() {
uploadImageClicked();
});
function uploadImageClicked() {
jQuery('#product_image').click();
}
dd($request->hasFile('product_image') returns false
In Laravel (which should be tagged in your question) it's not part of the Request class with the other form values, it's in the file method.
See https://laravel.com/docs/5.3/requests#retrieving-uploaded-files
$file = $request->file("image"); is what you're looking for.

Using PHPExcelReader to parse file and store values in array - File not found

I am using PHPExcelReader to parse an excel file (chosen by the user from their local machine). PHP will take those values and store them in an array and then reorganize it and display it on the screen in the form of a table.
However, after I browse for a file, once I click "Submit", I get the error: File not found
Here is the HTML:
<div class="row-fluid" style="margin-top:15px">
<h2>Step 1: Import the file</h2>
<p>Once uploaded, the window will display a preview of the document. Please check to make sure the column
headers match the data before uploading.</p>
<form action="../scripts/previewFile.php" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" />
<input type="submit" value="Preview File" name="submit">
<div id="filePreview" style="height: 500px; overflow: scroll;">
</div>
</form>
</div>
The previewFile.php file that is called:
function previewFile()
{
$filePath = $_FILES['file']['tmp_name'];
$excel = new Spreadsheet_Excel_Reader; // creates object instance of the class
$excel->read($filePath);
}
UPDATE
The error was a 404 for the PHP file "previewFile.php" that is called as an action.
Not sure what's wrong with my URL "../scripts/previewFile.php"
Here's my schema:
root
|_views
|_default
|_assets
|_scripts
|_previewFile.php
|_templates
|_step1.php <- the html file with the input tags
Your input name is fileToUpload, and you are looking in the $_FILES array for file, so it should be as follows:
function previewFile()
{
$filePath = $_FILES['fileToUpload']['tmp_name'];
$excel = new Spreadsheet_Excel_Reader; // creates object instance of the class
$excel->read($filePath);
}

Categories