Video Upload with php with mysql - php

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"];
}

Related

How to compress the size of the video file using php, while uploading to the server which supports ffmpeg?

<?php
error_reporting(1);
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "demo";
$conn = new mysqli($servername, $username, $password, $dbname);
$target_dir = "test_upload/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
if(isset($_POST["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'];
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],$target_file);
$sql = "INSERT INTO video (video_name) VALUES ('$video_path')";
$result = $conn->query($sql);
echo "uploaded ";
}
}
if(isset($_POST["disp"]))
{
$sql = "select * from video";
$result = $conn->query($sql);
while($row=mysqli_fetch_array($result,MYSQLI_NUM))
{ ?>
<video width="300" height="200" controls>
<source src="test_upload/<?php echo $row[1]; ?>" type="video/mp4">
</video>
<?php
}
} ?>
<html>
<body>
<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>
</body>
I am trying to upload the video file to server which supports ffmpeg , for small size videos like 4 to 5 mb it works fine , but when i am trying to upload the video more than 15mb, it takes too much time for upload.How to compress the size of the video file with out losing the quality while uploading to server using php?
you can’t. Php is excicuited on the server, not the client. It would need to be compressed before it is sent, otherwise it would take the exact same bandwidth.

Uploaded image using php doesn't fully work

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.

Uploading a video caus $_POST/$_FILES to be empty

I am trying to upload the image and video in my project, but when I tried to upload video it returns an empty array for $_POST and $_FILES both.
If I try to upload an image, the same code is working fine. The code I have used is below:
extract($_POST);
$upload_dir = wp_upload_dir();
$target_dir = $upload_dir['basedir'].'/mediauploads/';
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
if($upd)
{
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if($imageFileType != "mp4" && $imageFileType != "wmv" && $imageFileType != "avi" && $imageFileType != "mov" && $imageFileType != "3gp" && $imageFileType != "mpeg" && $imageFileType != "jpg")
{
echo "File Format Not Suppoted";
} else {
$video_path=$_FILES['fileToUpload']['name'];
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],$target_file);
echo "uploaded ";
}
}
Upload video form:
<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"/></td>
</tr>
</table>
</form>
if (isset($_FILES["stPic"]["name"]) && $_FILES["stPic"]["tmp_name"] != ""){
$fileName = $_FILES["stPic"]["name"];
$fileTmpLoc = $_FILES["stPic"]["tmp_name"];
$fileType = $_FILES["stPic"]["type"];
$fileSize = $_FILES["stPic"]["size"];
$fileErrorMsg = $_FILES["stPic"]["error"];
if (!preg_match("/.(gif|jpg|png|mp4|3gp|wmw|avi|mkv)$/i", $fileName) ) {
echo "File Format Not Suppoted";
}else{
$moveResult = move_uploaded_file($fileTmpLoc, "video/$fileName");
if ($moveResult != true) {
echo 'file not upload';
exit();
}
else{
//You run sql query
echo 'Uploaded';
}
}
}

PHP Post in URL without using GET

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'];

Video uploading using MySQL and PHP

<?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

Categories