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.
Related
This is my first question here, I hope I can explain my problem. I have PHP code where I want to upload a PDF file and select a date, then upload it to a folder in my project called "pdfs". Then I want to download it but only until the date I chose at the upload is reached.
My upload doesn't work and I don't know why, here is my code:
<?php
include_once 'headeradmin.php';
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css/styleUpload.css">
<title>Umleitung hochladen</title>
</head>
<body>
<div class="container">
<div class="row">
<form action="umleitungen.php" method="post" enctype="multipart/form-data">
<h3>Umleitungen hochladen</h3>
<input type="file" name="fileToUpload" id="fileToUpload" accept="application/pdf"><br>
<p> Datum: <input type="date" name="ablaufdatum"></p> <br>
<button type="submit" name="save">Hochladen</button>
</form>
</div>
</div>
</body>
</html>
<?php
if (isset($_POST['submit'], $_POST['ablaufdatum']) && is_uploaded_file($_FILES['fileToUpload']['tmp_name'])) {
$date = strtotime($_POST['ablaufdatum']);
if (false === $date) {
return;
}
$mimeType = mime_content_type($_FILES['fileToUpload']['tmp_name']);
$allowedFileTypes = ['application/pdf'];
if (!in_array($mimeType, $allowedFileTypes, true)) {
return;
}
$destination = 'pdfs/' . date('Y-m-d', $date) . '_' . time() . '.pdf';
if (!file_exists($destination)) {
if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $destination)) {
echo 'Supi :)';
} else {
echo 'Upload fehlgeschlagen :/';
}
}
}
include_once 'footer.php';```
Check that the web server has permissions to write to the "pdfs/" directory.
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.
I need to upload the image to a server and store to the directory created by PHP using current timestamp. Below is the relevant part of the code, but not working as expected, it's not creating a folder as well it's not printing to the console. What could be the issue?
Edit:
Modified php based on below comment
upload.php
<?php
//get unique id
$up_id = uniqid();
?>
<?php
//process the forms and upload the files
if ($_POST) {
//specify folder for file upload
$user = "user";
//console.log("debug.......");
echo "debug.......";
if (!file_exists("/var/www/Scan")) {
mkdir("/var/www/Scan", 0777, true);
}
$folderName = date("Y-m-d") . "_" . date("h_i_sa") . "_" . $user;
if (!file_exists("/var/www/Scan/$folderName")) {
mkdir("/var/www/Scan/$folderName", 0777, true);
}
$folder = "/var/www/Scan/$folderName";
//specify redirect URL
$redirect = "upload.php?success";
//upload the file
move_uploaded_file($_FILES["file"]["tmp_name"], "$folder" . $_FILES["file"]["name"]);
//do whatever else needs to be done (insert information into database, etc...)
//redirect user
header('Location: '.$redirect); die;
}
//
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Upload your file</title>
<!--Progress Bar and iframe Styling-->
<link href="style_progress.css" rel="stylesheet" type="text/css" />
<!--Get jQuery-->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.js" type="text/javascript"></script>
<!--display bar only if file is chosen-->
<script>
$(document).ready(function() {
//
//show the progress bar only if a file field was clicked
var show_bar = 0;
$('input[type="file"]').click(function(){
show_bar = 1;
});
//show iframe on form submit
$("#form1").submit(function(){
if (show_bar === 1) {
$('#upload_frame').show();
function set () {
$('#upload_frame').attr('src','upload_frame.php?up_id=<?php echo $up_id; ?>');
}
setTimeout(set);
}
//document.getElementById("message").innerHTML = "";
});
//
});
</script>
</head>
<body>
<div id="outPopUp">
<h1 >Upload your file </h1>
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
<br />
<br />
<!--Choose a file to upload<br />-->
<!--APC hidden field-->
<input type="hidden" name="APC_UPLOAD_PROGRESS" id="progress_key" value="<?php echo $up_id; ?>"/>
<!---->
<!-- <input name="file" type="file" id="file" size="30"/>-->
<label class="custom-file-upload">
<input name="file" type="file" id="file" onclick="myFunction()" />
Choose Video
</label>
<!--Include the iframe-->
<br />
<br />
<iframe id="upload_frame" name="upload_frame" color= black frameborder="0" border="0" src="" scrolling="no" scrollbar="no" > </iframe>
<br />
<!---->
<br />
<input class="btn btn-blue" name="Submit" type="submit" id="submit" value="Submit" />
<br />
<br />
<?php if (isset($_GET['success'])) { ?>
<span style="color:#FFFFFF;" id="message" class="notice">Your file has been uploaded.</span>
<?php } ?>
</form>
</div>
<script>
function myFunction() {
document.getElementById("message").innerHTML = "";
}
/*document.getElementById('file').onchange = function () {
//alert('Selected file: ' + this.value);
var path = this.value;
var fileName = path.replace(/.*(\/|\\)/, '');
alert('Selected file: ' + fileName);
//myFunction(fileName);
};*/
</script>
</body>
</html>
try a simple example for understanding:
<?php
//php content
if ($_POST) { //here we are checking $_POST values that $_POST has some values.
//specify folder for file upload
$tempDir = __DIR__ . DIRECTORY_SEPARATOR . 'upload'; //it means make a directory uploads where this php file is kept.
if (!file_exists($tempDir)) { // if $tempDir is not there, so it will create that directory.
mkdir($tempDir);
}
if(!empty($_FILES))//now checking your uploaded file is not empty
{
$nm=$_FILES['file']['name']; //here $nm get the name of the file
$tmp=$_FILES['file']['tmp_name'];//$tmp get the temporary file stored path
$mDir = __DIR__ . DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR .time().$nm; //this your destination path with time+nameof file as a name of file.
if(move_uploaded_file($tmp,$mDir)) //uploading file to your destination path, if uploaded then it will go in the if scope and echo.
{
echo "file uploaded with timestamp in uploads folder";
//now redirect from here any where
}
else
{
echo "fail to upload a file";
}
}
}
?>
I'm new to php and I followed a tutorial that shows how to upload a video file.
At this moment it uses move_uploaded_file function but it doesn't work, the file is not shown in "videos" folder. Can somebody explain to me why the file isn't showing up?
<html>
<head>
<title>Video Upload System</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<?php
include "connect.php";
?>
<div id='box'>
<form action="index.php" method="POST" enctype="multipart/form-data">
<?php
if(isset($_FILES['video'])){
$name = $_FILES['video']['name'];
$type = explode('.', $name);
$type = end($type);
$size = $_FILES['video']['size'];
$random_name = rand();
$tmp = $_FILES['video']['tmp_name'];
if($type != 'mp4' && $type != 'MP4' && $type != 'flv'){
$message = "Video Foramt Not Supported!";
}else{
move_uploaded_file($tmp, 'videos/'.$random_name.'.'.$type);
$message = "Successfully Uploaded";
}
echo "$message <br/><br>";
}
?>
Select Video: <br/>
<input type='file' name='video' />
<br/><br/>
<input type='submit' value='Upload' />
</form>
</div>
<div id='box'>
<?php
?>
</div>
</body>
</html>
you can check if uploaded successfully..
if(move_uploaded_file($tmp, 'videos/'.$random_name.'.'.$type)) {
$message = "Successfully Uploaded";
}
I suspect the path you have provided is not valid, tough.
I think that you need to check the php.ini file to see the upload file (video) size limit and increase it, or just try to upload a small size file.
i have written a code but that is not working. below is a code which enters an image into the database on user input. But my problem is that when i am calling it to print on page, nothing is diaplaying, please help me here.
<?php
$conx=mysqli_connect('localhost','root','sultan','colour');
$sql = "SELECT theme from colors WHERE username='shail' ";
$sqli=mysqli_query($conx,$sql);
$row=mysqli_fetch_row($sqli);
$theme=$row[0];
if(isset($_POST['submit']))
{
$theme=$_POST['theme'];
$sql = "UPDATE colors SET theme='$theme' WHERE username='shail' ";
$sqli=mysqli_query($conx,$sql);
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>
body{ background-image:url(<?php echo $theme ?>) ;}
</style>
</head>
<body>
<form method="post">
Input your picture<input type="file" name="theme" /><br />
<input type="submit" value="upload" name="submit" />
</form>
</body>
</html>
You need to terminate the statement.
current
<?php echo $theme ?>
new
<?php echo $theme; ?>
<?php
$conx=mysqli_connect('localhost','root','','colour');
$sql = "SELECT theme from colors WHERE username='shail' ";
$sqli=mysqli_query($conx,$sql);
$row=mysqli_fetch_row($sqli);
$theme=$row[0];
if(isset($_POST['submit']))
{
// new code
$path = '/';
$fileName = $_FILES['theme']["name"];
$fileTmpLoc = $_FILES['theme']["tmp_name"];
$temp = explode(".", $_FILES['theme']["name"]);
$extension = end($temp);
$temp = explode('.', $fileName );
$ext = array_pop($temp );
$name = implode('.', $temp );
$FileNM = $name.time().".".$extension;
$theme = $FileNM.$ext;
move_uploaded_file($_FILES['theme']["tmp_name"], $theme);
$sql = "UPDATE colors SET theme='$theme' WHERE username='shail' ";
$sqli=mysqli_query($conx,$sql);
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>
body{ background-image:url(<?php echo $theme; ?>) ;}
</style>
</head>
<body>
<form method="post" enctype="multipart/form-data">
Input your picture<input type="file" name="theme" /><br />
<input type="submit" value="upload" name="submit" />
</form>
</body>
</html>
<?php
$conx=mysqli_connect('localhost','root','','colour');
$sql = "SELECT theme from colors WHERE username='shail' ";
$sqli=mysqli_query($conx,$sql);
$row=mysqli_fetch_row($sqli);
$theme=$row[0];
if(isset($_POST['submit']))
{
// new code
$error1="";
$error2="";
if ($_FILES["theme"]["type"] !== "image/jpeg"){
echo $error1 = "File is not in JPG!";
}else{
$error1="";
}
if(($_FILES["theme"]["size"] > 1024)){
echo $error2 = "File size not allowed more than 1 MB!";
}else{
$error2="";
}
if($error1 =="" && $error2==""){
$fileName = $_FILES['theme']["name"];
$fileTmpLoc = $_FILES['theme']["tmp_name"];
$temp = explode(".", $_FILES['theme']["name"]);
$extension = end($temp);
$temp = explode('.', $fileName );
$ext = array_pop($temp );
$name = implode('.', $temp );
$FileNM = $name.time().".".$extension;
move_uploaded_file($_FILES['theme']["tmp_name"], "img/".$FileNM);
$sql = "UPDATE colors SET theme='$FileNM' WHERE username='shail' ";
$sqli=mysqli_query($conx,$sql);
}
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>
body{ background-image:url(<?php echo "img/".$FileNM; ?>) ;}
</style>
</head>
<body>
<form method="post" enctype="multipart/form-data">
Input your picture<input type="file" name="theme" /><br />
<input type="submit" value="upload" name="submit" />
</form>
</body>
</html>