I have the following PHP code in which i am creating a post with title description and image and i am storing image in a folder images. all things working fine while i create a post this move file to images folder. title , description and imagepath gone to news folder and so on. but when firstly arrive on createnews.php page this error is shown how to handle this error.
NOTE: this error is shown on page load after loading post is submitting correctly
Error
Notice: Undefined index: fileToUpload in E:\Software projects Folder\htdocs\Criclite\createnews.php on line 63
Sorry, there was an error uploading your file.
createnews.php
<!DOCTYPE html>
<html>
<head>
<title>Add news</title>
</head>
<body>
<form action="createnews.php" method="post" enctype="multipart/form-data">
<h2>Add Post Details Here</h2>
<?php if (isset($_GET['error'])) { ?>
<p class="error"><?php echo $_GET['error']; ?></p>
<?php } ?>
<label>Title</label>
<input type="text" name="title" placeholder="Enter Post Title"><br>
<label>Description</label>
<br>
<textarea rows="8" cols="100" name="description"></textarea>
<br><br>
<label >Select image:</label>
<input type="file" name="fileToUpload"
value="" />
<div>
<button onclick="allnews()" type="submit"name="upload">
Submit
</button>
</div>
</form>
</body>
</html>
<?php
include("db_config.php");
require_once "header.php";
$target_dir = "images/";
$file_name = date("U").".jpg";
$target_file = $target_dir . $file_name;
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
$title=$_POST['title'];
$description=$_POST['description'];
$sql= "
INSERT INTO `news` ( `title`, `description`, `image`) VALUES ( '".$title."', '".$description."', '".$target_file."')";
if ($link->query($sql) === TRUE) {
echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
}
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
This may not be a clean approach but a quick one. You can just add the following snippet to make sure that the form submission script only executes when the form is posted. You can add this at the beginning of the script.
<?php
if(!isset($_POST["submit"])){
return false;
}
...
Related
I made an upload form called upload.php with action up.php so I upload images in the folder C:/xampp/htdocs/uploadimages and in the page upload.html I would like to make a dynamic slideshow containing all new and old pictures.
upload.php
<body>
<form action="up.php" method="post" enctype="multipart/form-data">
<h2>Upload File</h2>
<label for="fileSelect">Filename:</label>
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
<p><strong>Note:</strong> Only <b> .jpg </b> and <b> .png </b> formats allowed to a max size of 3 MB.</p>
</form>
<div class="gallery">
<div class="gallery-image">
<?php
$directory = "C:/xampp/htdocs/uploadimages";
$images = glob($directory . "/*.jpg");
foreach($images as $image)
{
echo " <img src=\"basename($image)\"> ";
echo "<br>" ;
}
?>
</div>
</body>
up.php
<?php
$target_dir = "C:/xampp/htdocs/uploadimages/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
if (file_exists($target_file)) {
echo "File already exists. <br>";
$uploadOk = 0;
}
if ($_FILES["fileToUpload"]["size"] > 3 * 1024 * 1024) {
echo "Your file is too large. <br>";
$uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "png") {
echo "Only JPG and PNG files are allowed. <br>";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "";
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded. <br>";
} else {
echo "There was an error uploading your file. <br>";
}
}
?>
I tried the basename($image) inside a for-each loop but nothing worked
I'm currently working on some code. Where you should be able to upload a file and select witch type of file it is.
I'm using urls to make an database entry on the upload page , so my link should look like
www.mydomain.domain?id=1&type=type
But php only gets the id because it uses get from the previous page.
So it looks like this
www.mydomain.domain?id=1&type=
So my question is how can I get the selection in the url?
I tried it with jQuery but I suck at it ;D.
My form code:
<?php
$datetype = $_POST['dateiart'];
echo $datetype;
$ek = $_GET['id'];
?>
<form action="upload.php?id=<?php echo $ek; ?>&type=<?php echo $datetype;?>" target="_blank" method="post" enctype="multipart/form-data" id="dateiauswahl">
Datei zum hochladen auswählen
<input type="file" name="fileToUpload" id="fileToUpload"> <br>
<input onclick="myFunction()" type="submit" value="Datei hochladen" name="submit"><br><br>
<input type="hidden" value="<?php echo $ek?>" id="id" name="submit"><br><br>
<label>Dateiart:
<select name="dateiart" form="dateiauswahl" size="5">
<option value="EK-Rechnung">EK-Rechnung</option>
<option value="Kaufvertrag">Kaufvertrag</option>
<option value="VK-Rechnung">VK-Rechnung</option>
<option value="Datenblatt">Datenblatt</option>
<option value="Sonstige">Sonstige</option>
</select>
</label>
</div>
</form>
upload.php
<?php
$pdo = new PDO('mysql:host=localhost;dbname=', '', '');
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$ek = $_GET['id'];
$dateiart = $_GET['type'];
echo $dateiart;
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 50000000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "pdf"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
$statement = $pdo->prepare("INSERT INTO Dateien (Link, EKNR, Datei_Bezeichnung) VALUES (:Link, :EKNR, :Datei_Bezeichnung)");
$result = $statement->execute(array('Link' => $target_file, 'EKNR' => $ek, 'Datei_Bezeichnung' => $dateiart));
}
?>
Pass parameters as hidden inputs instead of printing them in the query string of action URL of the form. Use htmlspecialchars function to prevent security issues.
<?php
if (!isset($_GET['id']) || !isset($_GET['type'])){
die('Missing parameters');
}
?>
<form action="upload.php" target="_blank" method="post" enctype="multipart/form-data" id="dateiauswahl">
Datei zum hochladen auswählen
<input type="hidden" name="id" value="<?php echo htmlspecialchars($_GET['id']) ?>">
<input type="hidden" name="type" value="<?php echo htmlspecialchars($_GET['type']) ?>">
....... other inputs
</form>
The in the upload.php script get them from $_POST superglobal.
$ek = $_POST['id'];
$dateiart = $_POST['type'];
I'm trying to upload a file (image) to sql table via pathfile but I keep getting these error "undefined index : image on line 3 and line 24". I already defined the image in the userpage. I'm not using BLOB because it consumes too much space. In my sql table, I simply set the image column as vachar. I already checked the php.ini file in xampp ,the upload is on and the maximum is 2MB upload file.Please help. tq.
Below is the userpage:
<?php
//useracc-test.php
//start session
session_start();
require 'connect-test.php';
include 'upload.php';
if(isset($_POST['username'])){
$userName = $_POST['username'];
$query = "SELECT id, name, username, telno FROM users WHERE username = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param('s', $userName);
$stmt->execute();
$res = $stmt->get_result();
$row = $res->fetch_array();
$_SESSION['id'] = $row['id'];
$_SESSION['name'] = $row['name'];
$_SESSION['username'] = $row['username'];
$_SESSION['telno'] = $row['telno'];
}
?>
<html>
<head>
<script type="text/javascript">
function MM_jumpMenu(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
</script>
</head>
<body>
<div id="apDiv3">
<p> </p>
<p> </p>
<p> </p>
<p><span class="TabbedPanelsContent">
<?php
echo $_SESSION['id']."<br/>";
echo $_SESSION['name']."<br/>";
echo $_SESSION['username']."<br/>";
echo $_SESSION['telno']."<br/>";
?>
<?php
if(isset($_POST['submit']))
{
$id = $_POST['id'];
$name2 = $_POST['name2'];
$color2 = $_POST['color2'];
$hobby2 = $_POST['hobby2'];
$radiobtn = $_POST['radiobtn'];
$image = $_POST['image'];
$stmt = $conn->prepare("INSERT INTO useradvert (id,name2,color2,hobby2,radiobtn,image) VALUES (?,?,?,?,?,?)");
$stmt->bind_param("isssss",$id,$name2,$color2,$hobby2,$radiobtn,$image);
$stmt->execute();
// $stmt->close();
// $conn->close();
//think of something later how to close and logout
/* <?php unset($_SESSION);
session_destroy(); ?> */
}
?>
</p>
<p> </p>
<form name="form2"
action="useracc-test.php" method="post" enctype="multipart/form-data">
<p> </p>
<table width="500" border="0">
<tr>
<td>category</td>
<td><select name="jumpMenu" id="jumpMenu" onChange="MM_jumpMenu('parent',this,0)">
<option value="useracc-test.php" selected>Category</option>
<option value="useracc-test.php">Members</option>
<option value="useracc-test2-jumpmenu.php">Non-members</option>
</select></td>
</tr>
<tr>
<td>ID:</td>
<td><input name="id" type="text" id="id" value="<?php echo $_SESSION['id']; ?>" ></td>
</tr>
<tr>
<td>Name:</td>
<td><input type="text" name="name2" id="name2"></td>
</tr>
<tr>
<td>Color</td>
<td><input type="text" name="color2" id="color2"></td>
</tr>
<tr>
<td>Hobby</td>
<td><input type="text" name="hobby2" id="hobby2"></td>
</tr>
<tr>
<td>Sex</td>
<td>male
<input type="radio" name="radiobtn" id="radio" value="male">
female
<input type="radio" name="radiobtn" id="radio2" value="female"></td>
</tr>
<tr>
<td>Image</td>
<td><input type="file" name="image" id="image"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" id="submit" value="submit"></td>
</tr>
</table>
<div align="center"></div>
<p> </p>
</form>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p>
</body>
</html>
below is the upload.php file where I run the upload script.
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["image"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image or not
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["image"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["image"]["size"] > 200000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["image"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
I'm getting error undefined index on line 3 and line 24.
error line 3 refers to below;
$target_file = $target_dir . basename($_FILES["image"]["name"]);
error line 24 refers to below;
if ($_FILES["image"]["size"] > 200000) {
All the code that depends on the post parameters should be inside the if isset($_POST['submit']) block. Otherwise, none of the other $_POST or $_FILE parameters will be set.
The other problem you're having is that you're posting to the wrong script. The form should say action = "upload.php".
<?php
if(isset($_POST["submit"])) {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["image"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image or not
$check = getimagesize($_FILES["image"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["image"]["size"] > 200000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["image"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
?>
I have the code to upload an image to the server, but the PHP code for saving the image path to the database has a problem.It does not update the database.
It shows the following errors and con not udate the database:
( ! ) Notice: Undefined index: fileToUpload in C:\wamp\www\hosty\Uploads.php on line 4
( ! ) Notice: Undefined index: datereg in C:\wamp\www\hosty\Uploads.php on line 8
( ! ) Notice: Undefined index: Description in C:\wamp\www\hosty\Uploads.php on line 9
( ! ) Notice: Undefined index: title in C:\wamp\www\hosty\Uploads.php on line 10
( ! ) Notice: Undefined index: fileToUpload in C:\wamp\www\hosty\Uploads.php on line 26
<?php require_once('Connections/Conn_host.php'); ?>
<?php
error_reporting(0);
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$datereg=$_POST['datereg'];
$Description=$_POST['Description'];
$title=$_POST['title'];
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}}
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
mysql_select_db($database_Conn_host, $Conn_host);
$query_rsUpload = "insert into infor(title,Description,userId,datereg,name,path) values('$title','$Description',,'','$datereg','$name','uploads/$name')";
$rsUpload = mysql_query($query_rsUpload, $Conn_host) or die(mysql_error());
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<p>
<label>Title
<input name="title" type="text" id="title" />
</label>
</p>
<p>
<label for="Description">Description</label>
<textarea name="Description" id="Description" cols="45" rows="5"> </textarea>
</p>
<p>
<input name="userId" type="hidden" id="hiddenField" />
<input name="datereg" type="hidden" id="hiddenField2" />
</p>
<p>Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</p>
</form>
</body>
</html>
<input type="file" name="image" size="50">
<input type="Submit" name="submitted" value="Upload"
<?php
if(isset($_POST['submitted']))
{
//connect to database
$file=$_FILES['image']['name'];
$file_tmp=$_FILES['image']['tmp_name'];
$path="your/path/".$file;
if(file_exists($path))
{
chmod($path,0755);
unlink($path);
}
if(move_uploaded_file($file_tmp,$path))
{
echo "File uploaded succesfully";
$sql = "UPDATE database ".
"SET path = '$path'";
}
}
?>
Hope this will solve your problem.
There's a error in your query.
$query_rsUpload = "insert into infor(title,Description,userId,datereg,name,path) values('$title','$Description',,'','$datereg','$name','uploads/$name')";
After $Description, there are two commas ,,.
I want to upload an file from form and when I click submit it uploads the file to the server and insert filename, filesize and filetype from that file. but I always get errors like:
Warning: "filesize(): "stat failed for "uploadedfile" in C:\wamp\www\Project2\members\uploaded.php on line 14"
Warning: filetype(): "Lstat failed for "uploadedfile" in C:\wamp\www\Project2\members\uploaded.php on line 15"
Here is my uploadfile.php:
<html>
<body>
<?php
session_start();
$username =$_SESSION["uname"];
?>
<title>Uploading Page</title>
<b>Hello again,<?php echo $username ?>. From here, you can select your image or file and upload our database.</b>
<form action="uploaded.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form></tr>
</table>
</form>
</body>
</html>
and here is my uploaded.php:
<html>
<body>
<title>Uploading Page</title>
<?php
session_start();
$username =$_SESSION["uname"];
?>
<?php
$con = mysqli_connect("localhost", "root", "", "webpage");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$filename = basename($_FILES["fileToUpload"]["name"]);
$filesize = filesize($_FILES["fileToUpload"]["name"]);
$filetype = filetype($_FILES["fileToUpload"]["name"]);
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "<br>File is an image - " . $check["mime"] . ".</br>";
$uploadOk = 1;
} else {
echo "<br>File is not an image.</br>";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "<br>Sorry, file already exists.</br>";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000000000000) {
echo "<br>Sorry, your file is too large.</br>";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "<br>Sorry, only JPG, JPEG, PNG & GIF files are allowed.</br>";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "<br>Sorry, your file was not uploaded.</br>";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
$sql=mysqli_query($con,"insert into datas(username,imagesnotes,size,type) values('$username','$filename','$filesize')");
if(!$sql)
{
echo "<br>there is a problem in MySQL please check again.</br>";
}
echo "<br>The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.</br>";
} else {
echo "<br>Sorry, there was an error uploading your file.</br>";
}
}
mysqli_close($con);
?>