There are multiple questions[duplicates] about this same error but I can't find a solution in any of them. I`m trying to save an image in my local directory but 'Undefined index' keeps showing up. I´m a noob in php and stack overflow. I would really appreciate any of your help.
HTML:
<form method="post" action="process.php" enctype="multipart/form-data">
<fieldset>
<div class="col-xs-12 col-sm-push-5 image-div form-control">
<div class="input-margin">
<input type="file" name="imagenIngreso" class="filestyle" onchange="readURL(this);" data-buttonText="Subir Imagen" data-buttonName="btn-primary" data-size="nr" data-buttonBefore="false">
</div>
<div>
<img class="image-preview" id="blah" src=""/>
</div>
</div>
<div class="col-xs-12 col-sm-12 submit-button">
<button type="submit" class="btn btn-primary btn-block" name="submit" value="ingreso">Ingresar</button>
</div>
</fieldset>
</form>
PHP:
// Previous Validations
else if($_POST['submit'] == 'ingreso') {
$fileName = $_FILES['imagenIngreso']['name']; // Line throwing error
$uploadDirectory = "images/uploadedImages/Ingresos/";
if($uploadedFileName!='') {
$targetPath=time().$uploadedFileName;
if(move_uploaded_file($_FILES['imagenIngreso']['tmp_name'], $uploadDirectory.$targetPath)) {
$queryIngresos = "INSERT INTO ingresos (fechaIngreso, responsable, proyecto, items, imagePath) VALUES (STR_TO_DATE('$fechaEgreso','%d/%m/%Y'), '$responsable', '$proyectoIngreso', '$itemsArray', '$targetPath')";
if(!mysqli_query($con, $queryEgresos)) {
echo "Error while uploading file";
die('Error :'.mysqli_error($con));
} else {
echo 'Upload done';
exit();
}
}
}
You can also find what error are you getting using the Error Codes available.
Put this and make a try.
<?php
if($_FILES['userfile']['error']) {
// handle the error
} else {
// process
}
?>
Related
I want to save image inside mysql database. How I can do that using php and angular??
Im using add-style.component.ts to upload image
imageUploadEvent(event){
let imageFile = event.target;
if(imageFile.files.length > 0){
console.log(imageFile.files[0]);
let formData = new FormData();
formData.append('file',imageFile.files[0]);
this._databaseService.insertImageData(formData).subscribe((msg) => {
console.log(msg);
},(error) =>{
console.log("Get Some Error");
console.log(error);
});
}
}
I use add-style.html as following
<div class="container">
<div class="row">
<div class="col s8 offset-2">
<div class="card-panel teal lighten-2">
<h4>Image Uploading</h4>
</div>
<form #imageform ="ngForm">
<input type="file" class="form-file-control" (change)="imageUploadEvent($event)">
<button type="submit" class="btn-lg btn-submit form-control" (click)="saveFormData()" >submit</button>
</form>
</div>
</div>
</div>
I use database.service.ts to call php
insertImageData(value){
return this._http.get("http://localhost/jeleena-api/test-img.php",value).pipe(map(res => {
console.log(res);
}));
}
Following is my php code
<?php
//Create Database Connection
include "db-connection.php";
if(!empty($_FILES['file'])){
$ext = pathinfo($_FILES['file']['name'],PATHINFO_EXTENSION);
$image = time().'.'.$ext;
if(move_uploaded_file($_FILES["file"]["tmp_name"],$image)){
$sql = "INSERT INTO test(img) VALUES('".$image."')";
$conn->query($sql);
echo "successfully uploaded";
}
else{
echo ' error ';
}
}
?>
Ok bear with me. You can use ngStyle for this. https://angular.io/api/common/NgStyle
You need to install the commonsmodule in your ngModule.
Now in your template you can do the following to show the image:
<div [ngStyle]="background-image: url('./assets/images/youImage.jpg');">
Now you are able to show your image based on a url. So what you can do is instead of saving the image to the sql just simple save the ./assets/images/youImage.jpg.
<div [ngStyle]="background-image: url(yourObject.image)>
And to show the image, you can make an object give it a property image and assign the string from the sql.
When i m uploading multiple image in database then i m getting error like:- Invalid argument supplied for foreach()
My Code:-
<form name="" method="post" enctype="multipart/form-data" action="add_venue_image_do.php">
<div class="col-lg-12" style=" margin-bottom:16px;">
<label>Venu Image</label>
<input multiple required type="file" name="multi_image" class="form-control">
<input id="venue_hidden_id" value="5" name="venue_hidden_id" type="hidden"> //value 5 indicate business id
<button type="submit" value="Submit" style="margin-top:15px;" class="btn btn-primary btn-sm pull-right submitMultiImg">Submit </button>
</div>
</form>
add_venue_image_do.php:-
when PHP code excute foreach() then it's throwing error Invalid argument supplied for foreach()
<?php
if(isset($_COOKIE['login']))
{
require("../../root/db_connection.php");
if(isset($_REQUEST['venue_hidden_id'])){
$venue_hidden_id=$_REQUEST['venue_hidden_id'];
foreach($_FILES['image']['tmp_name'] as $key => $tmp_name ): //getting error on this line
$file_name = $key.$_FILES['multi_image']['name'][$key];
$file_size =$_FILES['multi_image']['size'][$key];
$file_tmp =$_FILES['multi_image']['tmp_name'][$key];
$file_type=$_FILES['multi_image']['type'][$key];
$img_ext= pathinfo($file_name,PATHINFO_EXTENSION);
$q=$db->query("insert into venue_image(v_b_id,v_i_ext,v_img_created_date)
values($venue_hidden_id,'$img_ext',now())") or die("error");
$lastID= $db->lastInsertId();
$imageNewName=$lastID.".".$img_ext;
move_uploaded_file($file_tmp,"../../venue_image//".$imageNewName);
endforeach;
?>
< script >
alert('Record Updated Successfully');
window.location.replace('edit_venue.php?v_id=5');
< /script>
<?php
}
else {
?> < script >
window.location.replace('add_venue.php');
< /script>
<?php
}
}
else
{
header("location:../index.php");
}
?>
Change your html code
<input type="file" name="files[]" multiple/>
Your php code should be
foreach($_FILES["files"]["tmp_name"] as $key=>$tmp_name)
{
// image uploading code
}
Reference link
Your input name only allow for single parameters, if you want to support multiple files, you will need to change to
<input multiple required type="file[]" name="multi_image" class="form-control">
and loop with php
<?php
foreach ($_FILES['file'] as $key => $file) {
$tmpName = $file['tmp_name'];
}
I want to save images in mysql using PHP
but I keep getting an error saying Fatal error: Call to undefined function saveimage() in D:\xampp\htdocs\PHPv2.0\Clients\subreqv2.php on line 100
And this is my code:
<body>
<form method="post" enctype="multipart/form-data">
<br/>
<!-- img1 file browser -->
<div>
<input type="file" name="image1"/>
<br/>
</div>
<!-- img2 file browser -->
<div>
<input type="file" name="image2"/>
<br/>
</div>
<!-- img3 file browser -->
<div>
<input type="file" name="image3"/>
<br/>
</div>
<!-- img4 file browser -->
<div>
<input type="file" name="image4"/>
<br/>
</div>
<!-- img5 file browser -->
<div>
<input type="file" name="image5"/>
<br/>
</div>
<!-- img6 file browser -->
<div>
<input type="file" name="image6"/>
<br/>
</div>
<!-- img7 file browser -->
<div>
<input type="file" name="image7"/>
<br/>
</div>
<input type="submit" name="sumit" value="Upload">
</form>
<?php
if(isset($_POST['sumit']))
{
if(getimagesize($_FILES['image1']['tmp_name'])== FALSE)
{
echo "Please select an image.";
}
else
{
$image1=addslashes($_FILES['image1']['tmp_name']);
$name1=addslashes($_FILES['image1']['name']);
$image1=file_get_contents($image1);
$image1=base64_encode($image1);
$image2=addslashes($_FILES['image2']['tmp_name']);
$name2=addslashes($_FILES['image2']['name']);
$image2=file_get_contents($image2);
$image2=base64_encode($image2);
$image3=addslashes($_FILES['image3']['tmp_name']);
$name3=addslashes($_FILES['image3']['name']);
$image3=file_get_contents($image3);
$image3=base64_encode($image3);
$image4=addslashes($_FILES['image4']['tmp_name']);
$name4=addslashes($_FILES['image4']['name']);
$image4=file_get_contents($image4);
$image4=base64_encode($image4);
$image5=addslashes($_FILES['image5']['tmp_name']);
$name5=addslashes($_FILES['image5']['name']);
$image5=file_get_contents($image5);
$image5=base64_encode($image5);
$image6=addslashes($_FILES['image6']['tmp_name']);
$name6=addslashes($_FILES['image6']['name']);
$image6=file_get_contents($image6);
$image6=base64_encode($image6);
$image7=addslashes($_FILES['image7']['tmp_name']);
$name7=addslashes($_FILES['image7']['name']);
$image7=file_get_contents($image7);
$image7=base64_encode($image7);
saveimage();
}
}
//displayimage();
function saveimagesaveimage($name1,$image1,$name2,$image2,$name3,$image3,$name4,$image4,$name5,$image5,$name6,$image6,$name7,$image7)
{
$con=mysql_connect("localhost","root","");
mysql_select_db("dummy",$con);
$qry="INSERT INTO images (name1,image1,name2,image2,name3,image3,name4,image4,name5,image5,name6,image6,name7,image7) VALUES ('$name1','$image1','$name2','$image2','$name3','$image3','$name4','$image4','$name5','$image5','$name6','$image6','$name7','$image7')";
$result=mysql_query($qry,$con);
if($result)
{
echo "<br/>Image successfully uploaded.";
}
else
{
echo "<br/>Error in uploading image.";
}
}
/* function displayimage()
{
$con=mysql_connect("localhost","root","");
mysql_select_db("dummy",$con);
$qry="SELECT * FROM images";
$result=mysql_query($qry,$con);
while($row = mysql_fetch_array($result))
{
echo '<img height="100" width="100" src="data:image;base64,'.$row['image'].'">';
}
}
*/
?>
</body>
Can anyone point out to me where did I go wrong?
Note: nevermind the use of mysql instead of msqli or PDO I just have to make this work so I could base my main project here.
Thanks for the help in advance. I'll appreciate it.
At the end you are calling saveimage() function, while in the code you have written the function name saveimagesaveimage().
Not only that you are passing many parameters to the function where it is initialised but where you are calling the saveimage() function even with the wrong name, no parameters are passed to it.
I am trying to delete image from folder and database using php unlink function and delete query. Here is my code,problem is i will not delete the image from folder. Please help.
$filetmp=$_FILES["images"]["tmp_name"];
$filename=$_FILES["images"]["name"];
$filetype=$_FILES["images"]["type"];
$filepath= "photo2/".$filename;
move_uploaded_file( $filetmp,$filepath);
$data=array($page_id,$filepath);
$add=add_data_event($data);
foreach ($errors as $value)
{
echo $value;
}
if(isset($_GET['id']))
{
$id=$_GET['id'];
delete_event1($id);
unlink("photo2/".$filename);
header('location:hotel2_galery_event.php?page_id=2');
}
HTML
<div class="form-group has-info">
<label>Event Related images</label>
<input type="file" name="images"><br>
</div>
<button type="submit" class="btn btn-primary">
<span>SUBMIT</span>
</button>
Have a look at this,
http://www.codingcage.com/2014/12/delete-uploaded-files-from-folder-in-php.html
Hope this helps.
Sohail.
I have a first page of a form, and then I use jQuery to load the second part of the form. However, after I click submit on the form, nothing happens, the page is just stuck here. Any ideas?
jQuery:
$.ajax({
type: "POST",
url: "join_submit.php",
data: data,
success: function () {
$("#regform").load("submitTranscript.php");
}
});
submitTranscript.php:
<div id="regform>
<form id="uploadTranscript" action="uploadPDF.php" enctype="multipart/form-data" method="post">
<div class="separation">
<h3>Upload Transcripts</h3>
<div class = "row">
<div class="large-6 offset-2 columns">
<label for = "studid">Enter your student ID:</lable>
<input type="text" name="studid" id="studid"
</div>
<p>Please label your transcript with your user id (i.e. 123456.pdf).</p>
<div class="row">
<div class="large-6 offset-2 columns">
<input type="file" name="transcript" id="transcript">
</div>
</div>
<div class="buttonRow">
<div class="button" id="submit">Submit</div>
</div>
</div>
</form>
</div>
uploadPDF.php:
<?php
require_once("included.php"); //server info
$allowedExtensions = array("pdf");
$max_filesize = 20000;
$upload_path = "docs/transcripts/";
$filename = $_FILES["transcript"]["name"];
$filesize = $_FILES["transcript"]["size"];
$extension = $_FILES["transcript"]["type"];
if ($_FILES["transcript"]["error"] > 0) {
echo "Error: " . $_FILES["transcript"]["error"] . "<br />";
}
else if((in_array($extension, $allowedExtensions)) && ($filesize < $max_filesize)) {
move_uploaded_file($_FILES["transcript"]["tmp_name"], $upload_path . $filename);
}
else if($filesize > $max_filesize){
$fileSizeFail = true;
}
else {
$fileTypeFail = true;
}
?>
If I look into submitTranscript.php, You have coded following for submitting your form:
<div class="buttonRow">
<div class="button" id="submit">Submit</div>
</div>
But, you haven't inserted any submit button to submit the form. Div element cannot post or submit any form. So, I would suggest to put an input type submit button then try to submit your form via that button.
So the code will be:
<div class="buttonRow">
<div class="button" id="submit">
<input type="submit" name="form_submit" value="Submit" />
</div>
</div>
You are not giving any information about join_submit.php file. You have to check in firebug console present in Firefox browser what is the return value of your join_submit.php file. Whether it is going to success function or not. Then only you can track why it is not loading the second form. In chrome browser you can trace the ajax request by clicking F12 and then Network.
Hope it helps