Omnimaga

General Discussion => Technology and Development => Web Programming and Design => Topic started by: ruler501 on November 24, 2012, 12:15:44 am

Title: PHP Upload
Post by: ruler501 on November 24, 2012, 12:15:44 am
How can I get video upload set up? I tried using this script I found
Code: [Select]
<?php 
 $uploaded_size 
$_FILES[&#39;uploaded&#39;][&#39;size&#39;];
 
$uploaded_type $_FILES[&#39;uploaded&#39;][&#39;type&#39;];
 
$target "/home/omni/uploads";
 
$allowed =  array(&#39;mp4&#39;,&#39;wmv&#39; ,&#39;avi&#39;,&#39;flv&#39;);
 
$filename $_FILES[&#39;uploaded&#39;][&#39;tmp_name&#39;];
 
$ext pathinfo($filenamePATHINFO_EXTENSION);
 
$target $target basename$_FILES[&#39;uploaded&#39;][&#39;name&#39;]) ; 
 
$ok=1
 
 
//This is our size condition 
 
if ($uploaded_size 209715200
 { 
 echo 
"Your file is too large.<br>"
 
$ok=0
 } 
 
 
//This is our limit file type condition 
 
if (!in_array($ext,$allowed))
 { 
 echo 
"Only Video Files<br>"
 
$ok=0
 } 
 
 
//Here we check that $ok was not set to 0 by an error 
 
if ($ok==0
 { 
 Echo 
"Sorry your file was not uploaded"
 } 
 
 
//If everything is ok we try to upload it 
 
else 
 { 
  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("home/omni/uploads" $_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"];
      
}
    }
 } 
 
?>

and
Code: [Select]
<html><body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
 Please choose a file: <input name="uploaded" type="file" /><br />
 <input type="submit" value="Upload" />
 </form>
</body></html>
Though that doesn't seem to be working. When I try uploading a 10mb avi file it tells me that I need to use a video file. If I take out the condition it says I uploaded a file with no name that is 0kb. I have apache2 and php5. How can I get uploads working where people can only upload files <200mb and that are video files?

I've been trying to learn how to use php and html but I still don't really understand it so it would really help if someone could just explain how to get it set up and what script to use.
I'm hosting it on my server (http://test.ruler501git.tk/omni/upload.html)
Title: Re: PHP Upload
Post by: Rhombicuboctahedron on November 24, 2012, 12:24:08 am
I doubt this would work, because I believe this is entirely meant for YouTube Direct, but when I compiled it all, I could embed something in a simple html file that led to a YouTube widget.
I wonder if it would be possible to view the source and use it, but I know even less of web programming.
http://omnimagatvtest.appspot.com/omnimagatvtestexample.html
Title: Re: PHP Upload
Post by: ruler501 on November 24, 2012, 12:28:12 am
Unfortunately that just seems to link to a javascript file specific to youtube direct. Thanks for linking it though; It reminds me that I forgot to link to where I host this.
Title: Re: PHP Upload
Post by: Rhombicuboctahedron on November 25, 2012, 12:11:12 am
I just started PHP today, so I don’t know very much, but it seemed like the error had to do something with the file type. It didn’t want to register it as accepted.
Also, I think you might be able to upload text with this.
I found this and changed a few things, but it needs to be changed a bi too, because it was meant for images.

Code: [Select]
<html>
<form name="upload" method="POST" action="upload.php">
Title: (limit 50 characters)<br />
<input type="text" name="title" maxlength="32" style="width:200px;" /><br /><br />
Message: (YouTube limits 1000 characters)<br />
<textarea name="message" cols="60" rows="20"></textarea><br /><br />
Upload Image: (.mp4, .wmv, .avi, .flv, and less than 200MB in size.)<br />
<input type="file" name="image" id="image" style="color:#fff;" />
<br /><br />
<input type="submit" name="upload" value="Upload" />
</form>`
</html>
Title: Re: PHP Upload
Post by: stevon8ter on November 25, 2012, 05:23:04 am
If i can get on my computer today, i'll post what script i'm using, and i'll try to explain it
Title: Re: PHP Upload
Post by: ruler501 on November 25, 2012, 02:01:35 pm
Rhombicuboctahedron I know how to get text. I'd probably just ask for a description txt file uploaded alongside. The hard part is linking them together for the admins, though I have ideas on how to do that. Once I get file upload working I'll work on that.

stevon8ter that would be great if you could post that. I am a complete php/web programming noob.
Title: Re: PHP Upload
Post by: stevon8ter on November 25, 2012, 02:07:29 pm
Ruler, i have a little bit of experience, but more then enough to make file uploads, a little visitor board and admin function, so if you ever need help...

(I'll do the script in 2h or so.. Sorry
Title: Re: PHP Upload
Post by: ruler501 on November 25, 2012, 02:49:19 pm
Anything you can do to help would be great. I plan on learning just have never had the time. The only real thing we need is a way for videos to be uploaded and linked with a text file. Then for admins to be able to look at that. I was planning on using ftp for the admins and maybe folders to store the video and text together if I can find a way to make folders and upload files to them.
Title: Re: PHP Upload
Post by: stevon8ter on November 25, 2012, 03:03:00 pm
Do you mean make folders on your website or do you mean make a folder on the server?
And what do you mean with textfiles?
That you upload a video + textfile and that they have to be linked together?
Title: Re: PHP Upload
Post by: ruler501 on November 25, 2012, 03:05:23 pm
Do you mean make folders on your website or do you mean make a folder on the server
Create a folder on the server to store the video and text file in.
And what do you mean with textfiles?
That you upload a video + textfile and that they have to be linked together?
Yeah I was thinking they'd both go in a folder together inside of the uploads directory
Title: Re: PHP Upload
Post by: stevon8ter on November 25, 2012, 03:06:50 pm
And what do you mean with textfiles?
That you upload a video + textfile and that they have to be linked together?
Yeah I was thinking they'd both go in a folder together inside of the uploads directory
so you mean for every video that's uploaded... create a nex folder?
ie: if i upload 1st video, create a folder of itself called vid1 ?
Title: Re: PHP Upload
Post by: ruler501 on November 25, 2012, 03:08:33 pm
Yeah create a folder and put the video and text in it. I don't really care what the folders are named as long as they are all unique names
Title: Re: PHP Upload
Post by: stevon8ter on November 25, 2012, 03:16:46 pm
Ok, you need the mkdir() command for that, but i'll explain it later...
Title: Re: PHP Upload
Post by: stevon8ter on November 25, 2012, 04:18:17 pm
1) sorry for double posting but i concidrr this as an update

Html form:
Code: [Select]
<form action="uploader.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<input type="submit" name="submit" value="Submit" />
</form>

Php script:
Code: [Select]
<?php

$allowedExts 
= array(/*all allowed exts go here*/);
$extension end(explode("."$_FILES["file"]["name"]));
if ((
$_FILES["file"]["size"] < 10485760 /* file size*/) && in_array($extension$allowedExts))
  {
  if (
$_FILES["file"]["error"] > 0)
    {
    echo 
"Return Code: " $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    
$random_digit=rand(0000,9999);
    
$pathnew $random_digit $_FILES["file"]["name"];
    echo 
"Uploaded file name: " $pathnew "<br />";
    echo 
"Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";

$path=random_digit;

    if (
file_exists("../upload/ /*or whatever the upload dir is*/" $path "/" $pathnew))
      {
             
$lol 0;
while (
$lol === 0)
{
      
$random_digit=rand(0000,9999);
    
$pathnew $random_digit $_FILES["file"]["name"];

$path $random_digit;

if (
file_exists("../upload/ /* again whatever upload dir is*/" $path "/" $pathnew))
{
$lol 0;
}
else
{
$lol 1;
}
}
      }
    else
      {
     
mkdir($path);

move_uploaded_file($_FILES["file"]["tmp_name"],
      
"upload/" $path "/" $pathnew);
       }
    }
  }
else
  {
  echo 
"Invalid file";
  }
?>


I made a few changes to try to let it work with newdirections, but didn't test it
So please report all errors ;)
Title: Re: PHP Upload
Post by: ruler501 on November 25, 2012, 05:19:38 pm
When I use this I'm not getting anything as output. I get a blank page when trying to upload an avi file

Code: [Select]
<?php

$allowedExts 
= array(&#39;mp4&#39;,&#39;wmv&#39;,&#39;avi&#39;,&#39;flv&#39;/*all allowed exts go here*/);
$extension end(explode("."$_FILES["file"]["name"]));
if ((
$_FILES["file"]["size"] < 10485760 /* file size*/) && in_array($extension$allowedExts))
  {
  if (
$_FILES["file"]["error"] > 0)
    {
    echo 
"Return Code: " $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    
$random_digit=rand(0000,9999);
    
$pathnew $random_digit $_FILES["file"]["name"];
    echo 
"Uploaded file name: " $pathnew "<br />";
    echo 
"Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";

$path=random_digit;

    if (
file_exists("/home/omni/uploads" $path "/" $pathnew))
      {
             
$lol 0;
while (
$lol === 0)
{
      
$random_digit=rand(0000,9999);
    
$pathnew $random_digit $_FILES["file"]["name"];

$path $random_digit;

if (
file_exists("/home/omni/uploads" $path "/" $pathnew))
{
$lol 0;
}
else
{
$lol 1;
}
}
      }
    else
      {
     
mkdir($path);

move_uploaded_file($_FILES["file"]["tmp_name"],
      
"/home/omni/uploads" $path "/" $pathnew);
       }
    }
  }
else
  {
  echo 
"Invalid file";
  }
?>
Did I do something wrong?
Title: Re: PHP Upload
Post by: stevon8ter on November 25, 2012, 05:23:04 pm
First question, is the php part in a different file as the html form?
2nd: is the php part in the uploader.php file?
Title: Re: PHP Upload
Post by: ruler501 on November 25, 2012, 05:24:41 pm
yes I have html file upload.html with the form and then uploader.php with the php code
Title: Re: PHP Upload
Post by: stevon8ter on November 25, 2012, 05:25:53 pm
hmmmmm let me think...
(Is it already on a server or on xampp or so?)

1st : put " " in the allowedexts array instead of ' '
2nd try removing all coments ( /*...*/ )
Title: Re: PHP Upload
Post by: ruler501 on November 25, 2012, 05:27:58 pm
its at http://test.ruler501git.tk/omni/upload.html
Title: Re: PHP Upload
Post by: stevon8ter on November 25, 2012, 05:31:44 pm
Are you sure you saved it correct the script? Cause when i look in the source it's an empty file...
Title: Re: PHP Upload
Post by: ruler501 on November 25, 2012, 05:34:01 pm
I saved it as a uploader.php with exactly what I posted earlier. When I execute php5 uploader.php from the command line it runs and tells the index is invalid and that its an invalid file
Title: Re: PHP Upload
Post by: stevon8ter on November 25, 2012, 05:36:10 pm
Hmmm i tested my script and for me it worked :/
But remove all comment lines...
And at the mkdir($path);

Do mkdir("uploaddir/" . $path);
Title: Re: PHP Upload
Post by: ruler501 on November 25, 2012, 05:48:37 pm
I'm still getting a completely blank page. Could it be something on my server set up wrong? This http://test.ruler501git.tk/omni/phptest.php page works so it seems not to be a problem with php. What else could cause it to show a completely blank screen?
Title: Re: PHP Upload
Post by: stevon8ter on November 25, 2012, 05:57:49 pm
Hmmmmmmm i don't really know

But if you trust me, send me all data needed to login to server (in a pm) and i'll look into it...
(I hope you trust me, i'll do nothing wrong)
Title: Re: PHP Upload
Post by: Rhombicuboctahedron on November 25, 2012, 07:50:23 pm
I think it might be working. I uploaded a test file.
Title: Re: PHP Upload
Post by: epic7 on November 25, 2012, 07:52:55 pm
I got
lolReturn Code: 1
Title: Re: PHP Upload
Post by: ruler501 on November 25, 2012, 07:58:16 pm
Rhombicuboctahedron your file went through. What kind of file were you trying to upload epic7? Though you should probably ask stevon8ter since he set it up
Title: Re: PHP Upload
Post by: epic7 on November 25, 2012, 08:02:29 pm
It was an avi file.

/me uploads an mp4 file
Says
Return Code: 1
Title: Re: PHP Upload
Post by: ruler501 on November 25, 2012, 08:13:54 pm
I forgot to change a seetting I'll fix it in a little bit.
Title: Re: PHP Upload
Post by: Rhombicuboctahedron on November 25, 2012, 08:26:25 pm
Yeah, I got the same when I uploaded a .avi file, but you should probably be able to change it easily enough.
Also, do you think you might be able make an upload progress bar, such as this one
http://www.johnboy.com/php-upload-progress-bar/
Sorry to be demanding, especially since you just finished making this, but that is certainly nice to look at while a big file is uploading.
Title: Re: PHP Upload
Post by: ruler501 on November 25, 2012, 08:40:25 pm
I'd ask stevon8ter about that I'm not gonna touch the script now that its working.

EDIT: The server is installing updates. When its done everything should be working
Title: Re: PHP Upload
Post by: epic7 on November 25, 2012, 08:45:28 pm
yaaaaay, works now.
At least it tells the percent uploaded in the corner of the screen (in chrome anyways)

And it tells the file size down to microbytes... how does that work :P
Title: Re: PHP Upload
Post by: ruler501 on November 25, 2012, 08:48:07 pm
Oh. The server just rebooted and is now installing updates so it may or may not work at any moment for the next 30 min.
Title: Re: PHP Upload
Post by: stevon8ter on November 26, 2012, 11:25:03 am
I'd ask stevon8ter about that I'm not gonna touch the script now that its working.

EDIT: The server is installing updates. When its done everything should be working

Yes i'm not touching it anymore ;p
Title: Re: PHP Upload
Post by: epic7 on November 26, 2012, 06:10:41 pm
Are ya gonna prettify the upload page, though? :P
Title: Re: PHP Upload
Post by: ruler501 on November 26, 2012, 10:10:16 pm
yeah I'll add some stuff to the upload page to make it a little prettier, and stevon8ter I hate to ask but could you add it so that they can upload a .txt document that goes in the same folder as the video?
Title: Re: PHP Upload
Post by: stevon8ter on November 27, 2012, 11:25:41 am
yeah I'll add some stuff to the upload page to make it a little prettier, and stevon8ter I hate to ask but could you add it so that they can upload a .txt document that goes in the same folder as the video?

yes i can, but do you mean uploading the same time the video uploads or uploading on another moment?
Title: Re: PHP Upload
Post by: Rhombicuboctahedron on November 27, 2012, 01:47:59 pm
Wouldn’t it be easier to have a browse for video file and a browse for text file?
Title: Re: PHP Upload
Post by: stevon8ter on November 27, 2012, 02:08:22 pm
yes that's the idea...
But i need to know if the browse for text file has to be on the same page as the browse for video, cause it could be he wants it to be like: post video... a week later... post text file associated with that video...
Title: Re: PHP Upload
Post by: Rhombicuboctahedron on November 27, 2012, 02:11:50 pm
Oh, I meant to type "on the same page".
Why wouldn’t he want it on the same page?
Title: Re: PHP Upload
Post by: stevon8ter on November 27, 2012, 02:15:31 pm
i don't know

but i'll code it, gimme a sec

EDIT: it's done
(ruler, could you please delete the files i uploaded without opening them? I'dd appreciate that, i just clicked my first .txt and it was something you shouldn't read please...)
Title: Re: PHP Upload
Post by: ruler501 on November 28, 2012, 06:22:05 pm
Sure I'll delete the stuff now. Thanks. Its working great.
Title: Re: PHP Upload
Post by: stevon8ter on November 29, 2012, 06:13:19 am
You're welcome ;)