I'm trying to upload an image using html and php but when I try it, it gives me always an error.
I've already seen in the php.ini file if the upload option is active and it is, also de max-size is at 1000M
HTML code
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<form action="php/uploadLocal.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>
</body>
</html>
PHP code
<?php
$target_dir = "images/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
if(isset($_POST["submit"])) {
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.";
}
}
?>
just restarted pc and it worked, simply frustrating
Related
I wanted to write a code which will let me upload a text file and then find the words repeated inside the file but my program is not even printing the lines. What to do?
This is upload.php file which will be reading and verifying the file received.
<?php
$target_dir = "text/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$fileType = pathinfo($target_file,PATHINFO_EXTENSION);
if (file_exists($target_file)) {
echo "Sorry, file already exists."."<br>";
$uploadOk = 0;
}
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large."."<br>";
$uploadOk = 0;
}
if($fileType != "txt" ) {
echo "Sorry, only txt files are allowed."."<br>";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded."."<br>";
}
else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_dir)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
$lines = file("$target_file");
foreach ($lines as $line_num => $line) {
echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
}
}
else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
This is index.php file from which I am sending the file as POST request.
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="assets/use.css">
<meta charset="utf-8">
<title>File Upload</title>
</head>
<body>
<h2>Upload a file for word matching</h2>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="fileToUpload" id="fileToUpload" required>
<br>
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>
everything is ok i can seperately insert query into my database but it does not move the uploaded file
here is my code. see please if you can find any issue below.
it checked my image folder directory it is correct the directory takes me to the exact folder.
upload_file is also on in php.ini
but still it is not working.
<?php
$conn = mysqli_connect("localhost", "root", "HERE_IS_MYCORRECT_PASSWORD", "HERE_IS_MYCORRECT_DTABASE_NAME");
if($conn) {
echo "<p style='color:green;'>connected</p>";
}
else {
echo " connection failed";
}
if(isset($_POST['uploadfilesub'])){
$id = $_POST['id'];
$filename = $_FILES['uploadfile']['name'];
$filetmpname = $_FILES['uploadfile']['tmp_name'];
$folder = './Resources/style/images/' . $filename;
echo "<p>".$filename."</p>";
echo "<p>".$filetmpname."</p>";
$sql = "INSERT INTO product_images VALUES ($id,'$filename') ";
if(move_uploaded_file($filetmpname , $folder)){
echo "<p color='green'>moved</p>";
$qry = mysqli_query($conn, $sql);
if($qry){
echo "<p color='green'>Inserted into mysql</p>";
} else {
echo "<p color='red'>Failed to Insert</p>";
}
}
else {
echo "<p style='color:red;'>Failed to move</p>";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="id">
<input type="file" name="uploadfile" />
<input type="submit" name="uploadfilesub" value="upload" />
</form>
</body>
</html>
As far as I can see your error (in regards to the particular, mentioned, issue) is very simple:
$folder = './Resources/style/images/';
change to:
$folder = './Resources/style/images/' . $filename;
Simply move_uploaded_file expects full target path and not only target folder.
For my project i am using PHP and MySql. In that i was tried to upload an image to the mysql database. In that i faced one terrible error. My html code was like this
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Learing new things</title>
<style>
body
{
margin:4%;
}
</style>
</head>
<body>
<form enctype="multipart/form-data" method=post name=imaging action="upload.php">
<input type="file" name=file><input type="submit" name=upload value=Upload>
</form>
</body>
</html>
"file" was the name of my file upload field. PHP code like this
<?php
$file=$_FILES['file'];
var_dump($file);
if(isset($_FILES['file']['name']))
{
echo "Image Uploaded";
echo $file['name'];
}
else
echo "Image not Uploaded";
?>
Whatever happen whether the file is uploaded or not uploaded, in my PHP page it is always executing the echo "image upload". i tried without selecting a file and clicked the upload still i am getting the same. How do i find whether a file is selected and uploaded in the html page. why i am getting the same message. it is not executing the else block.
Format html code
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Learing new things</title>
<style>
body
{
margin:4%;
}
</style>
</head>
<body>
<form enctype="multipart/form-data" method="post" name="imaging" action="upload.php">
<input type="file" name="file" /><input type="submit" name="upload" value="Upload" />
</form>"
</body>
</html>
Php check if file was selected
if (empty($_FILES['file']['name'])) {
// No file was selected for upload
}
Modify your Php script to this.
<?php
$target_dir = "(your target directory)/";
$target_file = $target_dir .basename($_FILES["uploadfile"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["upload"])) {
$check = getimagesize($_FILES["uploadfile"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
?>
I have a server apache and mysql at my friend's house, i can connect to both of them, but i want to upload pictures from my Macbook to my directory in the server (Ubuntu), i have some issues and tried many things but any of them worked, please help me thanks :)
The html form:
<html>
<head>
</head>
<body>
<form accept-charset="UTF-8" action="php.php" method="POST" enctype="multipart/form-data">
<input type="file" name="img"/>
<input type="submit"/>
</form>
</body>
</html>
The php :
<html>
<head> <meta charset="UTF-8"/> </head>
</html>
<?php
$bdd = new PDO('mysql:host=***.***.***.***;port:****;dbname=**********', '*******', '**********',array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
echo $_FILES['img']['name'];
$target1 = "/home/knight/";
$target = $target1 . basename( $_FILES['img']['name']);
if(move_uploaded_file($_FILES['img']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>
I just made a file upload code and I was wondering why the file upload wasn't working? I checked the permission of my image folder in localhost and that seems to be ok. I don't know where the problem exactly is. Any ideas?
<?php
require "config.php";
require "functions.php";
require "database.php";
if(isset($_FILES['fupload'])){
$filename = addslashes($_FILES['fupload']['name']);
$source = $_FILES['fupload']['tmp_name'];
$target = $path_to_image_directory . $filename;
$description = addslashes($_POST['description']);
$src = $path_to_image_directory . $filename;
$tn_src = $path_to_thumbs_directory . $filename;
if (strlen($_POST['description'])<4)
$error['description'] = '<p class="alert">Please enter a description for your photo</p>';
if($filename == '' || !preg_match('/[.](jpg)|(gif)|(png)|(jpeg)$/', $filename))
$error['no_file'] = '<p class="alert">Please select an image, dummy! </p>';
if (!isset($error)){
move_uploaded_file($source, $target);
$q = "INSERT into photo(description, src, tn_src)VALUES('$description', '$src','$tn_src')";
$result = $mysqli->query($q) or die (mysqli_error($myqli));
if ($result) {
echo "Succes! Your file has been uploaded";
}
createThumbnail($filename);
}
}
?><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Upload</title>
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<h1>My photos</h1>
<ul><?php getPhotos(); ?></ul>
<h2>Upload a photo</h2>
<form enctype="multipart/form-data" action="index.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<input type="file" name="fupload" /><br/>
<textarea name="description" id="description" cols="50" rows="6"></textarea><br/>
<input type="submit" value="Upload photo" name="submit" />
</form>
<?php
if (isset($error["description"])) {
echo $error["description"];
}
if (isset($error["no_file"])) {
echo $error["no_file"];
}
?>
</body>
</html>
Please make sure that you appended directory separator in $path_to_thumbs_directory.
And you don't need to use addslashes to $filename.