Jump to content

Recommended Posts

hi everyone

 

just registered as im stuck and hoping you guys can help, im new to php and mysql

and im trying to create a page that uploads files to a database. (largest file is 100meg)

 

but all others are below 20meg.

 

it only has to be simple, no restrictions on what you can upload , just the feature of uploading to the database and then being abel to download them files later.

 

ive followed this guide exactly .. pretty much using the same code.

 

http://php.about.com/od/phpbasics/ss/mysql_files_3.htm

 

but i get this error :(

 

Warning: fread(): supplied argument is not a valid stream resource

 

anyone know why?

 

or have a better guide that can teach me how to have a upload section? one specific file needs to be 128mb but the others are going to be below 20mb

 

your help is appreciated.

hi i will post my code later, as i dont have access to it atm, but it is pretty much the same as what is on that link.. ive just named stuff slightly different, but thats it

 

and i get the error.. i just wanted to trial and test it first before i do it properly

 

---

THE CODE

---

 

table

 

CREATE TABLE uploads (id INT(4) NOT NULL AUTO_INCREMENT PRIMARY KEY, description CHAR(50), data LONGBLOB, filename CHAR(50), filesize CHAR(50), filetype CHAR(50) );

 

form

 

<form method="post" action="upload.php" enctype="multipart/form-data">

Description:<br>

<input type="text" name="form_description" size="40">

<input type="hidden" name="MAX_FILE_SIZE" value="1000000">

<br>File to upload:<br>

<input type="file" name="form_data" size="40">

<p><input type="submit" name="submit" value="submit">

</form>

 

<?php

mysql_connect("localhost","root","admin");

mysql_select_db("practice");

 

$data = addslashes(fread(fopen($form_data, "r"), filesize($form_data)));

$result=MYSQL_QUERY("INSERT INTO uploads (description, data,filename,filesize,filetype) ". "VALUES ('$form_description','$data','$form_data_name','$form_data_size','$form_data_type')");

$id= mysql_insert_id();

print "<p>File ID: <b>$id</b><br>";

print "<p>File Name: <b>$form_data_name</b><br>";

print "<p>File Size: <b>$form_data_size</b><br>";

print "<p>File Type: <b>$form_data_type</b><p>";

print "To upload another file <a href=http://localhost/upload.php> Click Here</a>";

?>

 

 

btw. am using xampp to do this if that makes any difference

 

This code relies on register_globals to be enabled (which is discouraged as it poses a security risk).

 

You have to read data from form using $_POST variable

 

And I doubt that you will be able to insert 100MB file without changing max_packet_size setting in MySQL

Do you need to store the file in the DB? or can you just store the files in a directory and store the link

to that file in the DB?

 

 

o wow, that is actually what i wanted to do first, just didnt know how.

 

how would i go about doing this mate?

 

im going to have another go on it this weekend , so as much info as you can give :)

Use an upload page and a form page in PHP

I have used image files below change them to what you want ie I have used gif, jpeg, png etc.

 

If you would like to limit the file size or increase it in this value ($_FILES["file"]["size"] / 1024) change the 1024 to what you want.

 

This is the code for the form page so just make a page with the upload html or whatever script.

<?php 
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/png")
&& ($_FILES["file"]["size"] < 20000))
  {
  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("/your/website/directory" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "/your/website/directory" . $_FILES["file"]["name"]);
      echo "Stored in: " . "/your/website/directory" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
  $image = $_FILES["file"]["name"];
  $connect = mysql_connect connect to your database
$query = "INSERT INTO `tourist`(`image`) VALUES ('$image')";
$result = mysql_query($query) or die (mysql_error());
echo "$image";
  ?>

 

This should work as your upload page best of luck from England

 

 

This is really annoying me, i cant for the life of me , get it to work.

 

i keep getting invalid file :s

 

heres my upload code.. ive tried a simple upload with no restrictions and they say invalid file too

 

somebody help me :(

 

<?php

  //Include the connection details, open $connection and select database

include ("connection.php");

?>

    <?php

if ((($_FILES["file"]["type"] == "image/gif")

|| ($_FILES["file"]["type"] == "image/jpeg")

|| ($_FILES["file"]["type"] == "image/pjpeg"))

|| ($_FILES["file"]["type"] == "image/jpg")

|| ($_FILES["file"]["type"] == "image/png")

|| ($_FILES["file"]["type"] == "application/msword")

|| ($_FILES["file"]["type"] == "application/pdf")

|| ($_FILES["file"]["type"] == "application/ppt")

|| ($_FILES["file"]["type"] == "video/x-msvideo")

&& ($_FILES["file"]["size"] < 20000000000000000))

  {

  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"] / 10240000000000000) . " Kb<br />";

    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("/files/" . $_FILES["file"]["name"]))

      {

      echo $_FILES["file"]["name"] . " already exists. ";

      }

    else

      {

      move_uploaded_file($_FILES["file"]["tmp_name"],

      "/files/" . $_FILES["file"]["name"]);

        echo "Stored in: " . "/files/" . $_FILES["file"]["name"];

      }

    }

  }

else

  {

  echo "Invalid file";

  }

  $image = $_FILES["file"]["name"];

  $application = $_FILES["file"]["name"];

    $video = $_FILES["file"]["name"];

$query = "INSERT INTO `uploads`(`name`) VALUES ('$image')";

$query = "INSERT INTO `uploads`(`name`) VALUES ('$application')";

$query = "INSERT INTO `uploads`(`name`) VALUES ('$video')";

$result = mysql_query($query) or die (mysql_error());

echo "$image";

echo "$application";

echo "$video";

 

  ?>

 

That code is backwards. Unfortunately, it probably came from w3schools.com. They got the first example code right (it checks for an upload error first), but whoever added the ['type'] and ['size'] checking messed up and put it first, before the check for upload errors. Think about it, if the file did not upload and has an upload error, is the ['type'] or ['size'] going to be anything? That code will report an invalid file and won't ever report an upload error. And if the file is actually uploaded, the code should tell you exactly why the file is not valid. The invalid error should not combine the type/size in one test. It should tell you if the type is invalid and what the actual type was and it should tell you separately if the size was larger than allowed and what the actual size was.

 

For troubleshooting, add the following immediately after your first opening <?php tag -

 

echo "<pre>";
echo "POST:";
print_r($_POST);
echo "FILES:";
print_r($_FILES);
echo "</pre>";

  • 3 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.