Jump to content

[SOLVED] Having trouble with a script that is supposed to upload files


Recommended Posts

Hey everyone,

 

I have a script that is supposed to upload files to a folder on my webserver, and also write the info to a mysql table, it also writes the info to a csv file, now, here's what it does, it writes the data to the database, but doesn't upload the files, and doesn't write to the csv. Here's my code for anyone who would like to take a look:

 

<?php
// Receiving variables
@$pfw_ip= $_SERVER['REMOTE_ADDR'];
@$textfield = addslashes($_POST['textfield']);
@$textarea = addslashes($_POST['textarea']);
@$file_Name = $_FILES['file']['name'];
@$file_Size = $_FILES['file']['size'];
@$file_Temp = $_FILES['file']['tmp_name'];
@$file_Mime_Type = $_FILES['file']['type'];
@$file2_Name = $_FILES['file2']['name'];
@$file2_Size = $_FILES['file2']['size'];
@$file2_Temp = $_FILES['file2']['tmp_name'];
@$file2_Mime_Type = $_FILES['file2']['type'];

function RecursiveMkdir($path)
{
   if (!file_exists($path)) 
   { 
      RecursiveMkdir(dirname($path));
      mkdir($path, 0777);
    }
  }


// Validation
if (strlen($textfield) == 0 )
{
die("<p align='center'><font face='Arial' size='2' color='#000000'>Please enter the title of the video</font></p>");
}

if (strlen($textarea) == 0 )
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a description</font></p>");
}

if( $file_Size == 0)
{
die("<p align='center'><font face='Arial' size='2' color='#000000'>Please enter a file path</font></p>");
}
if( $file_Size >512000000)
{
//delete file
unlink($file_Temp);
die("<p align='center'><font face='Arial' size='2' color='#000000'>Please enter a file path</font></p>");
}
if( $file_Mime_Type != "application/x-shockwave-flash" )
{
unlink($file_Temp);
die("<p align='center'><font face='Arial' size='2' color='#000000'>Please enter a file path</font></p>");
}
$uploadFile =  $file_Name ;
if (!is_dir(dirname($uploadFile)))
  {
    @RecursiveMkdir(dirname($uploadFile)); 
  }
else
  {
  @chmod(dirname($uploadFile), 0777);
  }
@move_uploaded_file( $file_Temp , $uploadFile);
chmod($uploadFile, 0644);
$file_URL = "http://www.mysite.com/path/to/files/".$file_Name ;

if (strlen($file) == 0 )
{
die("<p align='center'><font face='Arial' size='2' color='#000000'>Please enter a file path</font></p>");
}

if( $file2_Size == 0)
{
die("<p align='center'><font face='Arial' size='2' color='#000000'>Please enter a file path</font></p>");
}
if( $file2_Size >1024000)
{
//delete file
unlink($file2_Temp);
die("<p align='center'><font face='Arial' size='2' color='#000000'>Please enter a file path</font></p>");
}
if( $file2_Mime_Type != "image/jpeg" )
{
unlink($file2_Temp);
die("<p align='center'><font face='Arial' size='2' color='#000000'>Please enter a file path</font></p>");
}
$uploadFile =  $file2_Name ;
if (!is_dir(dirname($uploadFile)))
  {
    @RecursiveMkdir(dirname($uploadFile)); 
  }
else
  {
  @chmod(dirname($uploadFile), 0777);
  }
@move_uploaded_file( $file2_Temp , $uploadFile); 
chmod($uploadFile, 0644);
$file2_URL = "http://www.mysite.com/path/to/files/".$file2_Name ;

if (strlen($file2) == 0 )
{
die("<p align='center'><font face='Arial' size='2' color='#000000'>Please enter a file path</font></p>");
}

//saving record in a text file
$pfw_file_name = "videolist.csv";
$pfw_first_raw = "textfield,textarea,file,file2\r\n";
$pfw_values = "$textfield,".str_replace ("\r\n","<BR>",$textarea ).",$file_Name,$file2_Name\r\n";
$pfw_is_first_row = false;
if(!file_exists($pfw_file_name))
{
$pfw_is_first_row = true ;
}
if (!$pfw_handle = fopen($pfw_file_name, 'a+')) {
die("Cannot open file ($pfw_file_name)");
exit;
}
if ($pfw_is_first_row)
{
  if (fwrite($pfw_handle, $pfw_first_raw ) === FALSE) {
  die("Cannot write to file ($pfw_filename)");
  exit;
  }
}
if (fwrite($pfw_handle, $pfw_values) === FALSE) {
  die("Cannot write to file ($pfw_filename)");
  exit;
}
fclose($pfw_handle);

//saving record to MySQL database


@$pfw_strQuery = "INSERT INTO `jos_video_player`(`title`,`description`,`file`,`thumbnail`)VALUES (\"$textfield\",\"$textarea\",\"$file_Name\",\"$file2_Name\")" ;
@$pfw_host = "*********";
@$pfw_user = "*****";
@$pfw_pw = "*******";
@$pfw_db = "********";
$pfw_link = mysql_connect($pfw_host, $pfw_user, $pfw_pw);
if (!$pfw_link) {
die('Could not connect: ' . mysql_error());
}
$pfw_db_selected = mysql_select_db($pfw_db, $pfw_link);
if (!$pfw_db_selected) {
die ('Can not use $pfw_db : ' . mysql_error());
}

//insert new record
$pfw_result = mysql_query($pfw_strQuery);
if (!$pfw_result) {
die('Invalid query: ' . mysql_error());
}
mysql_close($pfw_link);

echo("<p align='center'><font face='Arial' size='3' color='#FF0000'>Your upload was a success!</font></p>");
?>

 

I'd love it if anyone could help, it's probably something small I would guess?!?

 

Thanks!

To ease my pain and crank out some scripts that are on-the-fly, I use a form builder, it inserts all of the '@'s. As far as the code you speak of, I will check the docs, but I believe that a variable was set previously for that function. I'll check into it.

<?php

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

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

&& ($_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("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"];

      }

    }

  }

else

  {

  echo "Invalid file";

  }

?>

 

 

compare it works fine !!

 

 

I just noticed that you got that example from w3schools.com, which is fine, but a closer look at my code shows that I am trying to upload two files at once. This is an upload from a form, and the file directory is defined in the code. I'm not sure that the result is going to be any different regardless of the little snippet that I use. If you disagree, let me know, and why?

Well, I have no problem admitting that I was wrong ... and I was. When I removed the '@'s the thing worked beautifully. Thank you! Thank you! Thank you! I am definitely in your debt, I have been trying to get this piece of S**t running for two days, and I have no hair left on my head.

 

Thanks a million teng84!

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.