collapse collapse

* Who's Online

  • Dot Guests: 144
  • Dot Hidden: 0
  • Dot Users: 0

There aren't any users online.

* Board Stats

  • stats Total Members: 88
  • stats Total Posts: 11164
  • stats Total Topics: 1700
  • stats Total Categories: 4
  • stats Total Boards: 76
  • stats Most Online: 1470

Author Topic: PLEASE, PLEASE HELP! Need help with file uploader!  (Read 3136 times)

0 Members and 1 Guest are viewing this topic.

Offline ShadowIce

  • Newbie
  • *
  • Posts: 44
  • Reputation 62
    • View Profile
PLEASE, PLEASE HELP! Need help with file uploader!
« on: September 17, 2011, 02:40:36 pm »
Hi all, I need some help getting my PHP code to upload the BLOB files for itemimage and itemdata to my MySQL database. Problem is, it's saying the file does not exist after I hit upload. Why? How can I fix it?

Here is the small code:

Code: [Select]
<?php
function formatMoney($number$fractional=false) {
    if (
$fractional) {
        
$number sprintf('%.2f'$number);
    }
    while (
true) {
        
$replaced preg_replace('/(-?\d+)(\d\d\d)/''$1,$2'$number);
        if (
$replaced != $number) {
            
$number $replaced;
        } else {
            break;
        }
    }
    return 
$number;
}

//function to store uploaded file

function readfile_chunked$filename$retbytes true ) {
$chunksize * (1024 1024); // how many bytes per chunk
$buffer '';
$cnt 0;
$handle fopen$filename'rb' );
if ( $handle === false ) {
return false;
}
ob_end_clean(); //added to fix ZIP file corruption
ob_start(); //added to fix ZIP file corruption
header'Content-Type:' ); //added to fix ZIP file corruption
while ( !feof$handle ) ) {
$buffer fread$handle$chunksize );
//$buffer = str_replace("?","",$buffer);
echo $buffer;
ob_flush();
flush();
if ( $retbytes ) {
$cnt += strlen$buffer );
}
}
$status fclose$handle );
if ( $retbytes && $status ) {
return $cnt// return num. bytes delivered like readfile() does.
}
return $status;


function 
upload(){

global 
$whichdb;

$whichdb 1;

if(
$whichdb == 1){ $whichincludes "../inc/itemsconnlib.php"; }
if(
$whichdb == 2){ $whichincludes "../inc/imgconnlib.php"; }

if(
$whichdb == or $whichdb == 2){ include($whichincludes); }

include(
$whichincludes);

if(count($_FILES["item_file"]['name'])>0) { //check if any file uploaded
$GLOBALS['msg'] = ""//initiate the global message
for($j=0$j count($_FILES["item_file"]['name']); $j++) { //loop the uploaded file array
$file1 $_FILES["item_image"]['name']["$j"];
$file2 $_FILES["item_file"]['name']["$j"];
$filen2 $_POST['fname']["$j"]; //typed file name(s)
$filet get_file_extension($file2); //file type(s)
$filep formatMoney($_POST['fprice']["$j"], true); // file price(s)
$files $_FILES["item_file"]['size']["$j"]; //file size(s)

/*
$handle1 = fopen($file1, "r");
$contents1 = fread($handle1, filesize($file1));
fclose($handle1);
$handle2 = fopen($file2, "r");
$contents2 = fread($handle2, filesize($file2));
fclose($handle2);
*/

  $data1 $file1;
  $data2 $file2;
  $data1 mysql_real_escape_string($data1);
  $data2 mysql_real_escape_string($data2);
  
  //readfile_chunked($data1));
  

/*
$path = 'uploads/'.$filen; //generate the destination path
if(move_uploaded_file($_FILES["item_file"]['tmp_name']["$j"],$path)) { //upload the file
$GLOBALS['msg'] .= "File# ".($j+1)." ($filen) uploaded successfully<br>"; //Success message
}
*/

//Add to database

 $query     "INSERT INTO `$tbl` (itemimage, itemname, filetype, itemprice, itemdata)"
              ."VALUES('$data1', '$filen2','$filet','$$filep','$data2')" or die(mysql_error());

 $myquery mysql_query($query) or die(mysql_error());

$GLOBALS['msg'] .= "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"1000\" align=\"center\">\r\n"
."<tr>\r\n"
."<td width=\"300\" align=\"center\" valign=\"top\"><li>File# ".($j+1)."</li></td>\r\n"
."<td width=\"500\"><li>($file1)<br />uploaded successfully!</li></td>\r\n"
."</tr>\r\n"
    ."</table>\r\n";

}
}
else {
$GLOBALS['msg'] .= "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"1000\" align=\"center\">\r\n"
."<tr>\r\n"
."<td width=\"500\"><li>No files found to upload</li></td>\r\n"
."</tr>\r\n"
    ."</table>\r\n";
}
uploadForm(); //display the main form
}
session_destroy();
?>

Offline Nathan

  • Administrator
  • Hero Member
  • *
  • Posts: 1437
  • Reputation 1768
  • Gender: Male
  • woof woof
    • View Profile
Re: PLEASE, PLEASE HELP! Need help with file uploader!
« Reply #1 on: September 17, 2011, 10:38:14 pm »
What's the name of the form file upload?
Projects:
[ Axios Engine ] [ sourcehub ]
Compilers: Microsoft Visual Studio 2008, GNU C++, FASM, MASM, VB 6/.Net.
Languages: C++, PHP, ASM, JS, VB6/.Net, BASIC, HTML, MySQL
Please buy me some books: Amazon Wishlist

Offline ShadowIce

  • Newbie
  • *
  • Posts: 44
  • Reputation 62
    • View Profile
Re: PLEASE, PLEASE HELP! Need help with file uploader!
« Reply #2 on: September 18, 2011, 06:36:34 am »
<form name="frm" id="frm" method="post" onsubmit="return validate(this);" enctype="multipart/form-data">

Offline Nathan

  • Administrator
  • Hero Member
  • *
  • Posts: 1437
  • Reputation 1768
  • Gender: Male
  • woof woof
    • View Profile
Re: PLEASE, PLEASE HELP! Need help with file uploader!
« Reply #3 on: September 18, 2011, 10:51:54 am »
<form name="frm" id="frm" method="post" onsubmit="return validate(this);" enctype="multipart/form-data">

Can you post the whole form like <form> [including this stuff] </form>?
Projects:
[ Axios Engine ] [ sourcehub ]
Compilers: Microsoft Visual Studio 2008, GNU C++, FASM, MASM, VB 6/.Net.
Languages: C++, PHP, ASM, JS, VB6/.Net, BASIC, HTML, MySQL
Please buy me some books: Amazon Wishlist

Offline ShadowIce

  • Newbie
  • *
  • Posts: 44
  • Reputation 62
    • View Profile
Re: PLEASE, PLEASE HELP! Need help with file uploader!
« Reply #4 on: September 19, 2011, 08:39:15 am »
Code: [Select]
<form name="frm" id="frm" method="post" onsubmit="return validate(this);" enctype="multipart/form-data">
<input type="hidden" name="pgaction">
<br />
<?php if ($GLOBALS['msg']) { echo '<center><span class="err">'.$GLOBALS['msg'].'</span></center>'; }?>

                        <table align="center" cellpadding="4" cellspacing="0" bgcolor="#EDEDED">
                                        <tr class="tblSubHead">
                                                <td colspan="2">Upload any number of files</td>
                                        </tr>
                                        <tr class="txt">
<tr>
                                                <td align="center" colspan="2"><input type="submit" value="Upload File">&nbsp;<input type="reset" value="Clear"></td>
                                        </tr>
<tr>
<td><hr /></td>
</tr>
<tr>
<td>
<div class="container">
<div class="formHead">
<div class="fltlft">Upload any number of files</div>
<div class="fltrt"><a href="#" onclick="addField(this);">[+]</a> <a href="#" onclick="removeField('filediv');">[-]</a> <a href="#" onclick="javascript:showHelp();">[?]</a></div>
</div>
<strong>File Name(s):</strong><br />
<input name="fname[]" id="fname" type="text" /><br />
<strong>Choose file: </strong><br />
<input name="item_image[]" id="item_image" type="file"> <br />
<strong>File Image(s):</strong><br />
<input name="item_file[]" id="item_file" type="file"> <br />
<strong>File Price(s):</strong><br />
<input name="fprice[]" id="fprice" type="text" />
<div id="anchor"></div>
</div>
</td>
</tr>
<tr>
                                                <td align="center" colspan="2"><input type="submit" value="Upload File">&nbsp;<input type="reset" value="Clear"></td>
                                        </tr>
<tr>
<td><hr /></td>
</tr>
</table>


</form>

Offline Nathan

  • Administrator
  • Hero Member
  • *
  • Posts: 1437
  • Reputation 1768
  • Gender: Male
  • woof woof
    • View Profile
Re: PLEASE, PLEASE HELP! Need help with file uploader!
« Reply #5 on: September 21, 2011, 11:21:12 pm »
Hi all, I need some help getting my PHP code to upload the BLOB files for itemimage and itemdata to my MySQL database. Problem is, it's saying the file does not exist after I hit upload. Why? How can I fix it?

Here is the small code:

Code: [Select]
<?php
function formatMoney($number$fractional=false) {
    if (
$fractional) {
        
$number sprintf('%.2f'$number);
    }
    while (
true) {
        
$replaced preg_replace('/(-?\d+)(\d\d\d)/''$1,$2'$number);
        if (
$replaced != $number) {
            
$number $replaced;
        } else {
            break;
        }
    }
    return 
$number;
}

//function to store uploaded file

function readfile_chunked$filename$retbytes true ) {
$chunksize * (1024 1024); // how many bytes per chunk
$buffer '';
$cnt 0;
$handle fopen$filename'rb' );
if ( $handle === false ) {
return false;
}
ob_end_clean(); //added to fix ZIP file corruption
ob_start(); //added to fix ZIP file corruption
header'Content-Type:' ); //added to fix ZIP file corruption
while ( !feof$handle ) ) {
$buffer fread$handle$chunksize );
//$buffer = str_replace("?","",$buffer);
echo $buffer;
ob_flush();
flush();
if ( $retbytes ) {
$cnt += strlen$buffer );
}
}
$status fclose$handle );
if ( $retbytes && $status ) {
return $cnt// return num. bytes delivered like readfile() does.
}
return $status;


function 
upload(){

global 
$whichdb;

$whichdb 1;

if(
$whichdb == 1){ $whichincludes "../inc/itemsconnlib.php"; }
if(
$whichdb == 2){ $whichincludes "../inc/imgconnlib.php"; }

if(
$whichdb == or $whichdb == 2){ include($whichincludes); }

include(
$whichincludes);

if(count($_FILES["item_file"]['name'])>0) { //check if any file uploaded
$GLOBALS['msg'] = ""//initiate the global message
for($j=0$j count($_FILES["item_file"]['name']); $j++) { //loop the uploaded file array
$file1 $_FILES["item_image"]['name']["$j"];
$file2 $_FILES["item_file"]['name']["$j"];
$filen2 $_POST['fname']["$j"]; //typed file name(s)
$filet get_file_extension($file2); //file type(s)
$filep formatMoney($_POST['fprice']["$j"], true); // file price(s)
$files $_FILES["item_file"]['size']["$j"]; //file size(s)

/*
$handle1 = fopen($file1, "r");
$contents1 = fread($handle1, filesize($file1));
fclose($handle1);
$handle2 = fopen($file2, "r");
$contents2 = fread($handle2, filesize($file2));
fclose($handle2);
*/

  $data1 $file1;
  $data2 $file2;
  $data1 mysql_real_escape_string($data1);
  $data2 mysql_real_escape_string($data2);
  
  //readfile_chunked($data1));
  

/*
$path = 'uploads/'.$filen; //generate the destination path
if(move_uploaded_file($_FILES["item_file"]['tmp_name']["$j"],$path)) { //upload the file
$GLOBALS['msg'] .= "File# ".($j+1)." ($filen) uploaded successfully<br>"; //Success message
}
*/

//Add to database

 $query     "INSERT INTO `$tbl` (itemimage, itemname, filetype, itemprice, itemdata)"
              ."VALUES('$data1', '$filen2','$filet','$$filep','$data2')" or die(mysql_error());

 $myquery mysql_query($query) or die(mysql_error());

$GLOBALS['msg'] .= "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"1000\" align=\"center\">\r\n"
."<tr>\r\n"
."<td width=\"300\" align=\"center\" valign=\"top\"><li>File# ".($j+1)."</li></td>\r\n"
."<td width=\"500\"><li>($file1)<br />uploaded successfully!</li></td>\r\n"
."</tr>\r\n"
    ."</table>\r\n";

}
}
else {
$GLOBALS['msg'] .= "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"1000\" align=\"center\">\r\n"
."<tr>\r\n"
."<td width=\"500\"><li>No files found to upload</li></td>\r\n"
."</tr>\r\n"
    ."</table>\r\n";
}
uploadForm(); //display the main form
}
session_destroy();
?>

By taking a second look did you try taking the $j out of quotes? like:
$file1 = $_FILES["item_image"]['name'][$j];
instead of
$file1 = $_FILES["item_image"]['name']["$j"];
Because I'm pretty sure they are different keys.
Projects:
[ Axios Engine ] [ sourcehub ]
Compilers: Microsoft Visual Studio 2008, GNU C++, FASM, MASM, VB 6/.Net.
Languages: C++, PHP, ASM, JS, VB6/.Net, BASIC, HTML, MySQL
Please buy me some books: Amazon Wishlist

Offline ShadowIce

  • Newbie
  • *
  • Posts: 44
  • Reputation 62
    • View Profile
Re: PLEASE, PLEASE HELP! Need help with file uploader!
« Reply #6 on: September 22, 2011, 02:19:52 pm »
Not yet, but I will try. Thanks! :)

 

Donate


* Search


* Recent Posts

I miss the chatbox by Shishka
[August 27, 2019, 11:52:39 pm]


Image Comparison by Shishka
[May 15, 2017, 01:18:02 pm]


Re: srchub - free source code hosting by Nathan
[December 14, 2015, 11:37:02 pm]


Re: srchub - free source code hosting by Celestialkey
[November 27, 2015, 08:51:42 am]


Updates by Nathan
[October 30, 2015, 08:27:36 pm]