<?php
error_reporting(1);
$con=mysql_connect("localhost","root","");
mysql_select_db("demo",$con);
extract($_POST);
$target_dir = "test_upload/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
if($upd)
{
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if($imageFileType != "mp4" && $imageFileType != "avi" && $imageFileType != "mov" && $imageFileType != "3gp" && $imageFileType != "mpeg")
{
echo "File Format Not Suppoted";
}
else
{
$video_path=$_FILES['fileToUpload']['name'];
mysql_query("insert into video(video_name) values('$video_path')");
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],$target_file);
echo "uploaded ";
}
}
//display all uploaded video
if($disp)
{
$query=mysql_query("select * from video");
while($all_video=mysql_fetch_array($query))
{
?>
<video width="300" height="200" controls>
<source src="test_upload/<?php echo $all_video['video_name']; ?>" type="video/mp4">
</video>
<?php } } ?>
<form method="post" enctype="multipart/form-data">
<table border="1">
<tr>
<Td>Upload Video</td></tr>
<Tr><td><input type="file" name="fileToUpload"/></td></tr>
<tr><td>
<input type="submit" value="Uplaod Video" name="upd"/>
<input type="submit" value="Display Video" name="disp"/>
</td></tr>
</table>
</form>
Using this code I tried to upload video. Video name is properly saved in the database but problem is, that video does not move in the particular folder. So when I write the select query, that time video does not display.
Anyone, help me with the proper explanation.
I am new in programming language
First move to the folder and if moved then insert into the database. Then, Check the maximum upload size of your server.
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
mysql_query("insert into video(video_name) values('$video_path')");
echo "Uploaded successfully";
} else {
echo "Sorry, there was an error uploading your file.";
}
Change your php.ini file upload_max_filesize and post_max_size whatever the size you want. Example i given 100M - 100MB
; Maximum allowed size for uploaded files.
upload_max_filesize = 100M
; Must be greater than or equal to upload_max_filesize
post_max_size = 100M
Related
The problem is this one:
I have a code that uploads a picture to phpmyadmin, the code... doesn't fully work for some reason, it doesn't upload the image fully and the image ends up having a very small size, and it won't show up either. If I upload them directly to the database, they can be shown, but the ones uploaded using the code below have that problem.
This is the code I have:
Code for the button:
<div id="content">
<form method="POST" action =""/>
<table>
<tr>
<td>
Upload a picture
</td>
<td>
<input type="file" name="image"/>
</td>
</tr>
</table>
<input type="submit" name="submit" value="Registrarme"/> <input type="reset"/>
</form>
<?php
if(isset($_POST['submit'])){
require("iniciar.php");
}
?>
</div>
Code for the uploading: (iniciar.php)
<?php
$image = $_POST['image'];
$reqlen = strlen($image);
if($reqlen > 0){
$conn = mysqli_connect('localhost','root','','image');
$sql = "INSERT INTO `images` VALUES ('', '$image', '')";
mysqli_query($conn, $sql);
echo 'Upload successful';
}else{
echo 'Please insert an image';
}
?>
This is the structure of the table:
The table I have
you're assigning like input data instead of
$_FILES["image"]["name"];
//remove thisline
$image=$_POST['evidence'];
// you have to use it like this
$image= $_FILES["image"]["name"];
you have to assign image like this
$target_file = $target_dir . '/' . basename($_FILES["image"]["name"]);
$image=$target_file;
now evidence hold address of image so dir. of image is now on evidence too.now it will be uploaded to data base you aren't passing data. that's the mistake. any questions comment .if it solves your problem mark it as solved :)
for reference ,this is the php script used to upload profile picture,
<?php
if(isset($_POST["submit"]))
{
$target_dir = "profilepic/";
$targetfile=$target_dir.basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($targetfile,PATHINFO_EXTENSION);
if($imageFileType != "jpg" &&$imageFileType != "JPG" && $imageFileType != "png" && $imageFileType != "PNG" && $imageFileType != "jpeg" && $imageFileType != "JPEG" && $imageFileType != "gif" && $imageFileType != "GIF" )
{
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
if ($uploadOk == 0)
{
echo "<small>Sorry, your file was not uploaded.</small>";
}
else
{
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $targetfile))
{
echo "<small>upload successful</small>";
$imgdir="UPDATE user SET profilepic='$targetfile' WHERE username='$uname';";
$con->query($imgdir);
header("location:profile.php");
}
else
echo "not uploaded";
}
}
?>
make changes according to your data base & operations.
My uploaded file not to move the folder but path save in mysql. what have the problem for my code how to save the video in folder, ihave already create the folder name which is test_uploads. what is exexct method to get the video in folder
<?php
error_reporting(1);
$con=mysql_connect("localhost","root","");
mysql_select_db("ngo",$con);
extract($_POST);
$target_dir = "test_upload/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
if($upd)
{
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if($imageFileType != "mp4" && $imageFileType != "avi" && $imageFileType != "mov" && $imageFileType != "3gp" && $imageFileType != "wmv")
{
echo "File Format Not Suppoted";
}
else
{
$video_path=$_FILES['fileToUpload']['name'];
mysql_query("insert into video(video_name) values('$video_path')");
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],$target_file);
echo "uploaded ";
}
}
//display all uploaded video
if($disp)
{
$query=mysql_query("select * from video");
while($all_video=mysql_fetch_array($query))
{
?>
<video width="300" height="200" controls>
<source src="test_upload/<?php echo $all_video['video_name']; ?>" type="video/mp4">
</video>
<?php } } ?>
<form method="post" enctype="multipart/form-data">
<table border="1" style="padding:10px">
<tr>
<Td>Upload Video</td></tr>
<Tr><td><input type="file" name="fileToUpload"/></td></tr>
<tr><td>
<input type="submit" value="Uplaod Video" name="upd"/>
<input type="submit" value="Display Video" name="disp"/>
</td></tr>
</table>
</form>
Debug your code to find the error. Something like this:
$result = move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],$target_file);
if($result) {
mysql_query("insert into video(video_name) values('$video_path')");
echo "uploaded ";
} else {
echo $_FILES["file"]["error"];
}
I now have a code to upload a single file to my website, I want to change this to a multiple file upload. I want people to be able to select multiple files at once. Does someone know what I need to change about my code?
HTML
<html>
<body>
<form action="upload.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
<?php
$target_dir = "gallery-images/";
$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 "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["fileToUpload"]["size"] > 500000000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" && $imageFileType != "JPG" ) {
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.";
}
}
?>
All you need to make work it on front end is simply add MULTIPLE attribute to your input - it also works with drag and drop with couple browsers.
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload" multiple>
<input type="submit" value="Upload Image" name="submit">
</form>
Let me know if You want to send it one by one in ajax
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'];
hey I am not much of a PHP coder
I am using following to upload file to server acn any body help me whats wrong with this code
<?php
$uploaddir = './uploads/';
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "http://iphone.zcentric.com/uploads/{$file}";
}
?>
Thanx in advance
I don't see anything wrong with the PHP code, though without an error it is difficult to tell what is happening.
Somethings which could cause uploads not to work, and which may not return errors:
Ensure you have enctype="multipart/form-data in the form tag:
<form enctype="multipart/form-data" action="__URL__" method="POST">
Make sure PHP is accepting the input, by adjusting the following PHP ini variables:
http://us.php.net/manual/en/ini.core.php#ini.post-max-size
http://us.php.net/manual/en/info.configuration.php#ini.max-input-time
http://us.php.net/manual/en/ini.core.php#ini.upload-max-filesize
Finally, ensure that permissions are properly set for both the temp upload folder (http://us.php.net/manual/en/ini.core.php#ini.upload-tmp-dir) and the folder you are moving files to. If it is a Windows server you might also run into an inheritance issue which will require you to change the default upload directory.
iF YOU WANT TO UPLOAD .pdf FILE TO LOCAL SERVER THEN USE THIS SIMPLE METHOD, Lets we are doing code here under Button Click Event...
if (isset($_POST['submit']))
{
if ( ($_FILES["file"]["type"] =="application/pdf"))
{
if (file_exists("C:/xampplite/htdocs/site/upload/" . $_FILES["file"]["name"]))
echo " This File is already exists in folder";
else
{
move_uploaded_file ($_FILES["file"]["tmp_name"],"C:/xampplite/htdocs/site/upload/" . $_FILES["file"]["name"]);
echo "File have been Stored in:-C:/xampplite/htdocs/site/upload/ " . $_FILES["file"]["name"];
}
}
}//end of click_event
could u pls publish what the error u get?Your code looks ok.Here the upload folder must he stay in the upper of the directory where you run the code.Then it should to work.if your script folder like this /test/script/abc.php then your uploads directory should be /test/uploads.
index.php
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload[]" id="fileToUpload" multiple="">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
upload.php
<?php
//$target_dir = "uploads/";
/*$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
*/
if(count($_FILES['fileToUpload']['name']) > 0)
{
$i=0;
while($i<count($_FILES['fileToUpload']['name']))
{
$filen = $_FILES["fileToUpload"]['name']["$i"];
$path = 'uploads/'.$filen;
$imageFileType = pathinfo($path,PATHINFO_EXTENSION);
if (file_exists($path)) {
echo "Sorry, file already exists.";
}else if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
}
else if(move_uploaded_file($_FILES["fileToUpload"]['tmp_name']["$i"],$path))
{
//echo "The file ". basename( $_FILES["fileToUpload"]["name"]["$i"]). " has been uploaded.";
$files=$_FILES["fileToUpload"]["name"]["$i"];
echo $files;?><img src="<?php echo $path;?>" style="width:200px;height:200px" alt="" >
<?php
}
$i++;
}
}
?>