php .flv file uploading issue - php

well I can upload image, mp3, mp4, .doc file etc with the following form. But it's doesn't upload .flv file. Anyone can tell me what is the problem in my code or how can i upload .flv..
<?php
$mysql_connect = mysql_connect("localhost", "root" );
mysql_select_db("vedio");
ini_set('upload_max_filesize','1000M');
if(isset($_POST['action']) == "upload")
{
$name = $_FILES['file']['name'];
$tmp_name = $_FILES['file']['tmp_name'];
$size = $_FILES['file']['size'];
$type = $_FILES['file']['type'];
$name = str_replace(" ", "", $name);
$sql = mysql_query("INSERT INTO content VALUES('',
'$name', '$tmp_name', '$size' )");
if($sql)
{
echo "successfully uploaded";
}
else
{
echo "Something is wrong to upload";
}
$upload = "vedio/";
move_uploaded_file($_FILES['file']['tmp_name'], $upload . $name);
echo "<br/>";
echo $name;
echo "<br/>";
echo $tmp_name;
echo "<br/>";
echo $size;
echo "<br/>";
echo $type;
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"
enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="100000000000" />
<table width="400" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><input type="file" name="file" /></td>
<td><input type="submit" value="upload" name="action" /></td>
</tr>
</table>
</form>
Many Thanks.
Shibbir

My guess is your flv is probably just too big..
upload_max_filesize alone is not enough to set, you also need to set post_max_size otherwise your POST request will be empty and your upload will fail (it'll essentially look like a non-post request).

This line doesn't make sense
if(isset($_POST['action']) == "upload")
you are calling isset() which is going to return true or false depending upon if that variable is given a value and then you compare that true or false with the string "upload" which obviously would never be equal. You either need to check if the variable is set or check if the value is equal to "upload".
if(isset($_POST['action']) && $_POST['action'] == "upload")
Should also check for file upload errors. Something like:
if ($_FILES["file"]["error"] > 0) {
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else {
echo "No file upload errors";
}

Related

Updating wrong image using PHP MySQL

I am trying to allow admin to update photos of advertisements. I've managed to get the update part working, but it keeps updating the wrong advertisement (the first one in the database). I think it might not be registering the ID of the one I want to update when it is submitting to the database but I can't see what I have done wrong. I've used this code before for a profile picture update, but i'm guessing it's the session that is making it do this?
Here is my adupload.php code
<?php
$id=$_POST['id'];
$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 "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "<b><center>The file uploaded was not an image!<br><center>Go back here!";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "<b><center>Sorry, this image already exists! <br><center>Go back here!";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 50000000) {
echo "<b><center>Sorry, your image is too large.<br><center>Go back here!";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType
!= "jpeg"
&& $imageFileType != "gif" ) {
echo "<b><center>Sorry, only JPG, JPEG, PNG & GIF files are allowed.<br>
<center>Go back here!";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "<b><center>Sorry your picture was not uploaded.<br><center>Go back here!";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],
$target_file)) {
echo "<center>Your profile picture ". basename(
$_FILES["fileToUpload"]["name"]). " has been uploaded! <br><center>Go back to the admin area";
$sql = "UPDATE advertisement SET imageName='$target_file' WHERE
id=$id";
mysqli_query($mysqli_conn, $sql);
} else {
print "<b><center>An error occurred when uploading your picture<br>
<center>Go back here!";
}
}
?>
Here is a snippet of update_ad.php
$id = $_GET['id'] ; //Get the primary key passed
$query = "SELECT * FROM advertisement WHERE id = '$id' ";
//if error in query, display
$result = mysqli_query($mysqli_conn, $query) or exit ("Error in query:
$query. ".mysqli_error());
//see if any rows were returned
if (mysqli_num_rows($result) > 0) { // yes then display Form
$row = mysqli_fetch_assoc($result); //then fetch the row
?>
<table border= "3" style="width:100%;margin:auto">
<form action = 'update_ad_action.php' method="POST">
<tr><td>ID:</td><td><input readonly="yes" name="id" type="text" value=<?
php echo $id; ?> size="3" ></td></tr>
<tr><td>Title:</td><td><input type="text" name="title" value=<?php echo
$row["title"] ?> ></td></tr>
<tr><td>Description:</td><td><input type="text" name="description" value=
<?php echo $row["description"] ?> ></td></tr>
<tr><td>From-date:</td><td><input type="text" name="from_date" value=<?
php echo $row["from_date"] ?> ></td></tr>
<tr><td>To-date:</td><td><input type="text" name="to_date" value=<?php
echo $row["to_date"] ?> ></td></tr>
<tr><td>Pets:</td><td><select name="pets" id="pets">
<option value="0">Yes</option>
<option value="1">No</option></td></tr>
<tr><td>Location:</td><td><input type="text" name="location" value=<?php
echo $row["location"] ?> ></td></tr>
<tr><td>Picture</td><td><img src=" <?php echo $row['imageName'] ?>"
alt="No picture uploaded" width="200" height ="200" ></td></tr>
<tr><th colspan="2"><center><input class="button button-primary"
type="Submit" value="Submit"/>&nbsp<input class="button button-primary"
type="reset" value="Clear"/></center></th></tr>
</form>
</table>
<br>
<table border= "3" style="width:100%;margin:auto">
<form action="adupload.php" method="post" enctype="multipart/form-data">
<tr><td>Want to change your picture?</td><td>
<input type="hidden" name="id" value="<?php echo $_SESSION['id']; ?>">
<input type="file" name="fileToUpload" id="fileToUpload" required></td>
</tr>
<tr><th colspan="2"><center><input class="button button-primary"
type="submit" value="Upload Image" name="submit"></center></th></tr>
</form></table>
update_ad_action.php
<?php require 'config/init.php' ;
$id = $_POST['id']; //get id passed
$errorString = ""; //string to collect errors if found
$title = trim($_POST["title"]); //trim white spaces
if (empty($title)) {
$errorString = $errorString."<br><center>Please enter a title.
</center>"; //if empty, error added to error string
//remove any slashes that are automatically added, strip tags and
disable html tags from having any affect
$title = htmlentities(strip_tags($title));
}
$description = trim($_POST["description"]);
if (empty($description)) {
$errorString = $errorString."<br><center>Please enter a description.<
</center>"; //if empty, error added to error string
//remove any slashes that are automatically added, strip tags and
disable html tags from having any affect
$title = htmlentities(strip_tags($description));
}
$from_date = trim($_POST["from_date"]);
if (empty($from_date)) {
$errorString = $errorString."<br><center>Please enter a from date.
</center>"; //if empty, error added to error string
//remove any slashes that are automatically added, strip tags and disable
html tags from having any affect
$title = htmlentities(strip_tags($from_date));
}
$to_date = trim($_POST["to_date"]);
if (empty($to_date)) {
$errorString = $errorString."<br><center>Please enter a to date.
</center>"; //if empty, error added to error string
//remove any slashes that are automatically added, strip tags and disable
html tags from having any affect
$title = htmlentities(strip_tags($to_date));
}
$location = trim($_POST["location"]);
if (empty($location)) {
$errorString = $errorString."<br><center>Please enter a location</center>
"; //if empty, error added to error string
//remove any slashes that are automatically added, strip tags and disable
html tags from having any affect
$title = htmlentities(strip_tags($location));
}
// check if there were any errors
if (empty($errorString)) {//No errors, update the data
//query to update data
$query = "UPDATE user SET title = '$title', description = '$description',
from_date = '$from_date', to_date = '$to_date', location = '$location'
WHERE id = '$id'";
$result = mysqli_query($mysqli_conn, $query);
//if there was a problem - get the error message and go back
if (mysqli_affected_rows($mysqli_conn) < 0) {
echo "There were errors :<br>" . mysql_error();
echo $query;
exit ;
} else { //locate back to profile if updated
header("Location: admin.php");
}
} else {//There were errors
print '<b><center>There were errors<br>' . $errorString . "<center>Go back here!";
}
?>
The ID in your $_SESSION['id'] is likely to be wrong and thus you are sending always the same ID.
If you want to get the ID passed by URL (?id=1337), use <?php echo $_GET['id']; ?> in the form instead of $_SESSION.
where are you setting $_SESSION['id'] = $id? - if you are not setting that then this input will have no value.
<input type="hidden" name="id" value="<?php echo $_SESSION['id']; ?>">
you could change it to:
<input type="hidden" name="id" value="<?php echo $id; ?>">
also I am not loving the space in the name you posted
imageName='uploads/2015-10-04 19.59.41.jpg'
should really be with a hyphen or an underscore.:
imageName='uploads/2015-10-04_19.59.41.jpg'
you need ' ' around the variable in your update query in your adupload.php code:
$sql = "UPDATE advertisement SET imageName='$target_file' WHERE
id=$id";
should be
$sql = "UPDATE advertisement SET imageName='$target_file' WHERE
id='$id'";

Multiple Upload using CTRL Key PHP

would you help for my code , i need to do the multiple upload but i cant so will you help me please. i need so bad.
here's my code
i made multiple upload the form but it is not working. the output is "error array"
//HTML
<html>
<head>
<form name="Image" enctype="multipart/form-data" action="upload.php" method="POST">
<h1><font face="tahoma"> UPLOAD FILES</h1>
<label for="file">Filename:</label>
<input type="file" name="Photo[]" accept="image/*" multiple="multiple"/><br/><br/>
<input type="hidden" id="pageName" name="pageName">
<script type="text/javascript">
//get page name from parent
var value = window.opener.pageName
document.getElementById("pageName").value = value;
</script>
<INPUT type="submit" class="button" name="Submit" value=" Upload ">
<INPUT type="reset" class="button" value="Cancel"><br/><br/>
</form>
</head>
</html>
//PHP this is were upload is do.
<?php
include('global.php');
?>
<?
$uploadDir = 'directory/'; //Image Upload Folder
if(isset($_POST['Submit']))
{
$fileName = $_FILES['Photo']['name'][0];
$fileName1 = $_FILES['Photo']['name'][1];
$tmpName = $_FILES['Photo']['tmp_name'];
$fileSize = $_FILES['Photo']['size'];
$fileType = $_FILES['Photo']['type'];
$filePath = $uploadDir . $fileName . $fileName1;
//upload error
if ($_FILES["Photo"]["error"] > 0)
{
echo "Error: " . $_FILES["Photo"]["error"] . "<br />";
}
//photo already exixts
else
//insert image into DB
{
move_uploaded_file($tmpName, $filePath);
$filePath = addslashes($filePath);
$filePath = stripslashes($filePath);
$filePath = mysql_real_escape_string($filePath);
$query = "INSERT INTO images (image , category ) VALUES ('$filePath', '$pageName')";
mysql_query($query) or die('Error, query failed');
echo" Upload Successful. <br/> <br/>";
echo "Stored in: " . "directory/" . $_FILES["Photo"]["name"];
?>
<br/><br/>
<img width="300" height="400" src="directory /<?=$_FILES["Photo"]["name"]?>"><br/>
<?
}
}
?>
Error: Array is telling you that the error being returned is actually an Array object, not a string. If you want to see the actual error message, you need to view the full contents of the array. Something like print_r($_FILES["Photo"]["error"]); or looping through the array like so
foreach($_FILES["Photo"]["error"] as $err) {
echo "error: " . $err . "<br>";
}
Or you can just print the first error returned just as you have returned the first file in your name array echo $_FILES["Photo"]["error"][0];

Issue with updating image info on MYSQL database using PHP

I'm having problem with my code. I can't update my image.
Here is my full code:
index.php
//this link will post my data to next page
update
updatepage.php
//this page will get all data that want to update...
<?php
include("config.php");
$name=$_GET['code'];
$sql1 = "select * from imagename where name='$name'";
$result = mysql_query($sql1) or die(mysql_error());
$row=mysql_fetch_array($result);?>
<form name="form1" method="post" action="processupdatepage.php" class="formposition" enctype="multipart/form-data" >
<table>
<tr><td>
<input type="hidden" name="id" value="<?php echo $row['id']; ?>" /></td></tr>
<tr><td width="98">Image</td><td width="288">
//This is where I have problem, I can't get my image value but other data value work very well.....
<input type="file" name="image" value="<?php echo $row['image'];?>" /></td></tr><br/>
<tr><td>nane</td><td><input type="text" name="name" value="<?php echo $row['name']; ?>"/></td></tr><br/>
<tr><td></td> <td colspan="2">
<input type="submit" name="submit" value="Save" /> </td></tr>
</table>
</form>
OK. Now this my process file:
processupdatepage.php
<?php
include("config.php");
$id=$_POST['id'];
$image=$_FILES['image']['name'];
$name=$_POST['name'];
$target = "images/";
$target = $target . basename( $_FILES['image']['name']);
$query = "UPDATE imagename SET name='$name', image='$image' WHERE id='$id'";
$bb = mysql_query($query) or die(mysql_error());
if($bb)
{
//Writes the photo to the server
if(move_uploaded_file($_FILES['image']['tmp_name'], $target))
{
$sql001="UPDATE imagename SET image='$image' WHERE image='$image'";
mysql_query($sql001);
} else { }
header("Location:index.php");
}
else
{
echo "Could not be updated";
}
?>
I can update BUT must select image. If I'm not selecting an image, my image field in database will be empty BUT other data updates are OK...
The value of an <input type="file"> cannot be programatically changed. Only by triggering its default behaviour that pops up the select file dialogue.
What I see from you example code is that you want to associate the value to the file form control element straight as it was retrieved from the database. So why saving the same back. Still, if you want to do this, you can use a hidden element with the value, that will be saved. But if you don't want the user to change it, just don't render it into the form and leave it out from your update query so it will remain unchanged.
Also, you could add some logic when you process the post and leave the field 'image' out of your update query if no file was selected.
I think the problem is here:
<input type="file" name="image" value="<?php echo $row['image'];?>" />
You can upload the image by clicking the browse button. so what do you trying to do like that?
If you want to show the image already uploaded you can use :
$img_path = 'get image path from database entry';
<img src= "<?php echo $img_path; ?>"/>
Here is full code to upload an Image, this is tested and works but you need to have a directory called 'upload' (this code does not create the folder).
It is advisable to only store image path on database.
<?php
if(isset($_POST['submit'])){
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 200000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
if (file_exists("upload/" . $_FILES["file"]["name"])){
echo $_FILES["file"]["name"] . " already exists. ";
}
else{
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
// HERE YOU CAN WRITE YOU INSERT INTO MYSQL CODE
//SOMETHING LIKE THIS:
$path = 'assets/article_images/'.$article_id.'/'.$_FILES["image_file"]["name"];
$statement1 = "INSERT INTO image_article (article_id, image_path) values ('$article_id', '$path') ";
$query_result = mysql_query($statement1) or die($statement1."mysql error<br/><br/>".mysql_error());
if($query_result==1){
echo 'Image uploaded';
}
}
}
}
else
{
echo "Invalid file";
}
}
?>
<html>
<body>
<form action="index.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

How to upload an image and then store it in a database?

I am trying to Upload an image and store it in the database. I have mentioned the code, but it is not working for me. I'm not able to find the bug. Please see if anybody can find the bug.
The code is:
<?php
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
if($_REQUEST['submit'])
{
if(($_FILES['file']['type']=="image/jpeg" or $_FILES['file']['type']=="image/pjpeg" or $_FILES['file']['type']=="image/gif") and ($_FILES['file']['size']<300000))
{
if($_FILES['file']['error']>0)
{
$err=" ERROR :: ".$_FILES['file']['error'];
}
else
{
echo "Upload :".$_FILES['file']['name']."<br/>";
echo "Type: ".$_FILES['file']['type']."<br/>";
echo "Size: ".($_FILES['file']['size']/1024)."Kb<br/>";
echo "Stored in: ".$_FILES['file']['tmp_name']."<br/>";
move_uploaded_file($_FILES['file']['tmp_name'],"upload/".$_FILES['file']['name']);
echo $_FILES['file']['name'];
echo $_FILES['file']['tmp_name']."qwerty";
$n=3000;
$fo=fopen("upload/".$_FILES['file']['name'],"r");
$fr=fread($fo,$n);
mysql_query("update user set pic='$fr' where id='$id'");
}
}
else
{
$err=" ERROR :: Invalid Image";
}
mysql_query("update user set name='$name' where id='$id'");
mysql_query("update user set email='$email' where id='$id'");
}
$sql="select * from user where id='$id'";
$con=mysql_query($sql);
list($idR,$nameR,$emailR,$pwdR,$dateR,$picR)=mysql_fetch_array($con);
?>
<body>
<form action="" method="post" enctype="multipart/form-data">
<table>
<tr>
<td colspan="2"><?php echo $err; ?></td>
</tr>
<tr>
<td>Name:</td>
<td><input type="text" name="name" value="<?php echo $nameR ?>" /></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email" value="<?php echo $emailR ?>" /></td>
</tr>
<tr>
<td><label for="file">Upload or Change Pic:</label></td>
<td><input type="file" name="file" id="file" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" value="Save"/></td>
</tr>
</table>
</form>
</body>
My code is not even able to upload the file. When I tried to echo the following, I found them empty:
echo "Name:".$_FILES['file']['name']."<br/>";
echo "Type: ".$_FILES['file']['type']."<br/>";
echo "Size: ".($_FILES['file']['size']/1024)."Kb<br/>";
echo "Stored in: ".$_FILES['file']['tmp_name'];
All the above fields are empty.
I don't know how this work but it didn't worked. I guess the problem is somewhere in my HTML code because the $_FILES values are none/empty. It is not retrieving the value from the form tag please check if my answer is incorrect to solve the problem or correct to how to solved this hard problem from science or other subject in computers and it form to been error or successfully.
Try this!
<?php
if(isset($_POST['submit']) && $_FILES['file']['size'] > 0)
{
// Data
$file_name = $_FILES['file']['name'];
$tmp_name = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
// Check
$type = array(
'image/jpeg',
'image/gif',
'image/png',
);
$size = 300000;
if(in_array($file_type, $type) AND $file_size > $size)
{
echo 'Ooops! File type must be JPG, GIF or PNG. Or file size too big';
exit();
}
// Is error?
if ($_FILES["file"]["error"] > 0)
{
echo 'Upload Error';
exit();
}
else
{
echo 'File Name : '.$file_name.'<br />';
echo 'File Size : '.($file_size / 1024).' Kb<br />';
echo 'File Type : '.$file_type;
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" />
<input type="submit" name="submit" value="Save"/>
</form>
If $_POST and $_FILES are both empty then you've exceeded the maximum post size ini ('post_max_size')
There are several settings in php.ini, including the maximum for an individual file ('upload_max_filesize'), and the maximum for all uploads ('post_max_size'). Make sure those ini settings are set correctly
Be careful of syntax if you hve edited it, it's picky when handling M, K or G (not KB, MB or GB), and don't exceed the bit size of your server, i.e. don't go above 4billion bytes on a 32 bit server.
Other causes of $_FILES being empty, but not $_POST. You don't have enough free space to upload your files, or you don't have permissions ot write to the temp directory. Check /tmp has enough space, and that you can write to it as apache/php user.
It worked when I moved my code on an independent .php file. And when I needed to upload I gave the link to that page in which independent uploading code was written. I don't know the reason behind this but this is how I solved it.
hmm....
try to change this line
if($_REQUEST['submit'])
to be
if(isset($_REQUEST['submit']))
and i think, this code
if(($_FILES['file']['type']=="image/jpeg" or $_FILES['file']['type']=="image/pjpeg" or $_FILES['file']['type']=="image/gif") and ($_FILES['file']['size']<300000))
can be simplified into
if(ereg("jpeg|pjpeg|jpg|gif|png", $_FILES['file']['type']) AND $_FILES['file']['size']<300000)
Good luck...

How can I save an image from a file input field using PHP & MySQL?

How can I save an image safely from a file input field using PHP & MySQL?
Here is the input file field.
<input type="file" name="pic" id="pic" size="25" />
This is a simple example, it should work.
Although you probably want to add checking for image types, file sizes, etc.
<?php
$image = $_POST['pic'];
//Stores the filename as it was on the client computer.
$imagename = $_FILES['pic']['name'];
//Stores the filetype e.g image/jpeg
$imagetype = $_FILES['pic']['type'];
//Stores any error codes from the upload.
$imageerror = $_FILES['pic']['error'];
//Stores the tempname as it is given by the host when uploaded.
$imagetemp = $_FILES['pic']['tmp_name'];
//The path you wish to upload the image to
$imagePath = "images/";
if(is_uploaded_file($imagetemp)) {
if(move_uploaded_file($imagetemp, $imagePath . $imagename)) {
echo "Sussecfully uploaded your image.";
}
else {
echo "Failed to move your image.";
}
}
else {
echo "Failed to upload your image.";
}
?>
http://php.net/file_upload covers just about everything you need to know.
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$tmpFile = $_FILES['pic']['tmp_name'];
$newFile = '/new_location/to/file/'.$_FILES['pic']['name'];
$result = move_uploaded_file($tmpFile, $newFile);
echo $_FILES['pic']['name'];
if ($result) {
echo ' was uploaded<br />';
} else {
echo ' failed to upload<br />';
}
}
?>
<form action="" enctype="multipart/form-data" method="POST>
<input type="file" name="pic" />
<input type="submit" value="Upload" />
</form>

Categories