Log in

View Full Version : TMP File to MySQL?



Laim
08-16-2013, 02:01 AM
Basically, I've learned PHP, XML, HTML, CSS, Javascript etc however I never got down to learning MySQL and kind of just forgot about it. However a few days ago I got a client asking if I could make them a 'Video Host' so certain people could upload videos. (Much like YouTube.) She wanted Search etc and sadly, that needs MySQL. I had to reject her simply because I was to lazy to learn it. Now I'm learning it I'm completely stumped! :confused:

I decided to throw myself in the deep end and try to something like what she requested, so far I've achieved being able to log information in a database and put the file (movie.mp4) in a directory. However now I'd like to store the files in the directory too so I don't use a unneeded amount of server space. The code I currently have to upload the file and and store the information in the database (and the file in its directory) is as follows.



if(isset($_POST['submit']))
{
$name = $_FILES['file']['name'];
$temp = $_FILES['file']['tmp_name'];

move_uploaded_file($temp,"uploaded/".$name);
$url = "uploaded/$name";
mysql_query("INSERT INTO `videos` VALUE ('','$name','$url')");
}


I know it's not the nicest looking code, but for now I'd rather get it working then mess with how it looks. Anyway.

I was wondering if they're was anyway to move the TEMP file into the database? much like how you do it with the NAME of the file. Any help with this is much appreciated as I've been stuck at this since Yesterday.

Nile
08-16-2013, 02:15 AM
Typically, storing files inside databases is generally advised against. I could go through a few reasons, but you might as well just check out the quesion here (http://stackoverflow.com/questions/8952/storing-a-file-in-a-database-as-opposed-to-the-file-system) (you'll find reasons why not to store files inside databases, but also solutions if you decide to dig yourself into that whole).

Deadweight
09-29-2013, 05:56 AM
I have something that allows you to upload pictures (assuming you could do the same thing with videos)

index.php

<!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=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<form action="upload_rename_ac.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>

upload_rename_ac.php

<?php
$random_digit_one=rand(0,99999999);
$random_digit_two=rand(0,99999999);
$random_digit_fin=rand(0,99999999);
$allowedExts = array("gif", "jpeg", "jpg", "png"); //Change this to video files
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif") /*Change file type*/
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000000) /* change size */
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

if (file_exists("upload\\" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],"upload\\" . $random_digit_one.'_'.$random_digit_two.'_'.$random_digit_fin.'_'.$_FILES["file"]["name"]);
echo "Stored in: " . "upload\\" . $random_digit_one.'_'.$random_digit_two.'_'.$random_digit_fin.'_'.$_FILES["file"]["name"];
// echo '<div><image src="upload\\' . $random_digit_one.'_'.$random_digit_two.'_'.$random_digit_fin.'_'.$_FILES["file"]["name"].'" width="50%" /></div>';
echo '<a href="index.php">Return</a>';
}
}
}
else
{
echo "Invalid file";
}
?>

This shall uploading it. If you wanna add something to match the name with the video type and just throw the video name with the video uploaded i think that shall work. However, its late for me so i cant really check right now.

KolunDir
10-25-2013, 07:58 AM
Often this means your /tmp partition has run out of space and the file can't be created, or for whatever reason the mysqld process cannot write to that directory because of permission problems. Sometimes this is the case when selinux rains on your parade.

Any operation that requites a "temp file" will go into the /tmp directory by default. The name you're seeing is just some internal random name.