Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Unbale to upload video and PDF file to the folder
Index -> Programming, PHP -> PHP Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
andsonlhs




PostPosted: Sun Feb 26, 2012 3:38 am   Post subject: Unbale to upload video and PDF file to the folder

Can anyone help me why unable to upload video and PDF file to the folder?



<?php


$DBConnect = @mysqli_connect("localhost", "root", "")
Or die("<p>Unable to connect to the database server.</p>"
. "<p>Error code " . mysqli_connect_errno()
. ": " . mysqli_connect_error()) . "</p>";

$DBName = "educationtime";
if (!@mysqli_select_db($DBConnect, $DBName))
{
$SQLstring = "CREATE DATABASE $DBName";
$QueryResult = @mysqli_query($DBConnect, $SQLstring)
Or die("<p>Unable to execute the query.</p>"
. "<p>Error code " . mysqli_errno($DBConnect)
. ": " . mysqli_error($DBConnect)) . "</p>";

mysqli_select_db($DBConnect, $DBName);
}

$TableName = "tutorial";
$SQLstring = "SELECT * FROM $TableName";
$QueryResult = @mysqli_query($DBConnect, $SQLstring);
if (!$QueryResult) {
$SQLstring = "CREATE TABLE tutorial
(tutorialID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
subject VARCHAR(30),
chapter VARCHAR(30),
title VARCHAR(200),
video VARCHAR(200),
file VARCHAR(200),
register_date date,
register_time time)";

$QueryResult = @mysqli_query($DBConnect, $SQLstring)

Or die("<p>Unable to create the member table.</p>"
. "<p>Error code " . mysqli_errno($DBConnect)
. ": " . mysqli_error($DBConnect)) . "</p>";
}

//This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['video']['name']);
$target = $target . basename( $_FILES['file']['name']);



//This gets all the other information from the form
$subject =$_POST['subject'];
$chapter =$_POST['chapter'];
$title =$_POST['title'];
$video =($_FILES['video']['name']);
$file =($_FILES['file']['name']);


//Writes the information to the database
mysql_query("INSERT INTO tutorial VALUES ( NULL, '$subject', '$chapter', '$title', '$video','$file', now(), now())");

//Writes the photo to the server
if(move_uploaded_file($_FILES['video']['name'], $target, $_FILES['file']['name'], $target ))
{
$QueryResult = @mysqli_query($DBConnect, $SQLstring)
Or die("<p>Unable to execute the query.</p>"
. "<p>Error code " . mysqli_errno($DBConnect)
. ": " . mysqli_error($DBConnect)) . "</p>";
$tutorialID = mysqli_insert_id($DBConnect);

//Tells you if its all ok
echo "<script type='text/javascript'>alert('You have successfully uploaded a new tutorial.');</script><script>window.location = 'FileManager.php'</script>";
}
else {

//Gives and error if its not
echo "<script type='text/javascript'>alert('You have successfully uploaded a new tutorial.');</script><script>window.location = 'FileManager.php'</script>";
}
?>
Sponsor
Sponsor
Sponsor
sponsor
pierret




PostPosted: Thu May 24, 2012 7:20 pm   Post subject: RE:Unbale to upload video and PDF file to the folder

try updating javascript to newest version. maybe that will help.
bms




PostPosted: Fri Sep 21, 2012 3:46 pm   Post subject: Re: Unbale to upload video and PDF file to the folder

I know this is a necro, but I figured I'd give a bit of help incase someone decides to search for something like this.

File uploads are a fairly simple process. So, let's start with a simple form:

HTML:

<html>
    <body>
        <form action='upload.php' method='POST' enctype='multipart/form-data'>
            <table>
                <tr>
                    <td><label for='upload'>Choose a file:</label></td>
                    <td><input type='file' name='upload' id='upload' /></td>
                </tr>
                <tr>
                    <td><input type='submit' value='Upload' /></td>
                </tr>
            </table>
        </form>
    </body>
</html>


Note the enctype. Very important.

php:

<?php

// Some basic error handling
if(!$_FILES['upload'])
    die("No file found!");
elseif(!$_FILES['upload']['tmp_name'])
    die("Could not find uploaded temporary file! Possible permission error!");
elseif($_FILES['upload']['error'])
    die("Error: {$_FILES['upload']['error']}");
elseif(file_exists($_FILES['upload']['name']))
    die("A file with this name already exists.");

$tempFile = $_FILES['upload']['tmp_name'];
$actualName = $_FILES['upload']['name'];
$sizeInMB = round($_FILES['upload']['size'] / 1048576, 2);

if(move_uploaded_file($tempFile, $actualName))
    echo "
<html>
    <body>
        <h1>Results of file upload</h1>
        <table border='1'>
            <thead>
                <th>Originally Stored As</th>
                <th>Filename</th>
                <th>Size</th>
                <th>File Link</th>
            </thead>
                <tbody>
                    <td align='center'>$tempFile</td>
                    <td align='center'>$actualName</td>
                    <td align='center'>$sizeInMB mb</td>
                    <td align='center'><a href='$actualName'>$actualName</a></td>
            </tbody>
        </table>
    </body>
</html>"
;
else
    die("Unable to upload file!");


Now, there are a couple of things to note about the PHP. First of all, when a file is uploaded, it's stored temporarily at a location with a temporary name, seen in the table. So, basically, you have to move THAT file to the location you desire. So, the above code with all the files in the root directory will just upload the files to the root directory. So, fakedomain.com/test.jpg for example.

So you can see that, this:

php:

...
//Writes the photo to the server
if(move_uploaded_file($_FILES['video']['name'], $target, $_FILES['file']['name'], $target ))
{
...


is very wrong. First off, you're not even touching the actual file that has been uploaded and you are using the function in the wrong way. The definition of move_uploaded_file is:

c++:

bool move_uploaded_file ( string $filename , string $destination )


So, what you should've done is:

php:

...
if(move_uploaded_file($_FILES['video']['tmp_name'], $target))
...
if(move_uploaded_file($_FILES['file']['tmp_name'], $target ))
...


Now, ontop of this, you have:

php:

//This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['video']['name']);
$target = $target . basename( $_FILES['file']['name']);


So first off, you're trying to find the basename of a file that doesn't exist on the server at all. And secondly, if the file was there(it isn't), $target would be the value of the two paths combined. So, if basename( $_FILES['video']['name']) was "/tmp" and basename( $_FILES['file']['name']) was '/dev' then what $target would be is "/images/tmp/dev", which is obviously wrong and not what you wanted at all.

With move_uploaded_file, you need to supply two fullpaths to the files in the arguments of the function. You can't just pop in a filename and then a path and expect it to work. It won't. So, knowing what the functions do and what is required is very important.

I recommend you learn some basic programming logic. That $target snafu clearly demonstrates a lack of programming logic. After that, make sure you understand what a function does and how it works before you try to use it. Also learn what the constructs are available in a language and use them effectively.
Display posts from previous:   
   Index -> Programming, PHP -> PHP Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: