Jump to content

PHP upload file problem


daytonatkf

Recommended Posts

i'm using WAMP on Vista ultimate to develop this php system. I having this problem now. When i upload file/image from certain folder, it can upload. What i mean here is, when i upload file from 'c:wamp/www/myproject/folder', then the file can upload, if i upload from other than the mention folder, the error message appear

 

this is the pop up error message:

Message: Expected hexadecimal digit

Line: 124

Char: 156

Code: 0

URI: http://localhost/akd/u1.php?xf_table_na ... f_action='

 

and this is the error appear in the page itself

PHP-Warning: fopen(3.jpg) [function.fopen]: failed to open stream: No such file or directory in C:\wamp\www\akd\u1.php at 214

PHP-Warning: fread(): supplied argument is not a valid stream resource in C:\wamp\www\akd\u1.php at 218

 

and solution? thanks for reply

Link to comment
https://forums.phpfreaks.com/topic/144173-php-upload-file-problem/
Share on other sites

"PHP-Warning: fopen(3.jpg) [function.fopen]: failed to open stream: No such file or directory in C:\wamp\www\akd\u1.php at 214"

 

 

It can't open 3.jpg for some reason.  Why would the file not be there?  Perhaps when it's uploaded, copying it from the temporary directory isn't working?  Or perhaps permission is denied or something?

"PHP-Warning: fopen(3.jpg) [function.fopen]: failed to open stream: No such file or directory in C:\wamp\www\akd\u1.php at 214"

 

 

It can't open 3.jpg for some reason.  Why would the file not be there?  Perhaps when it's uploaded, copying it from the temporary directory isn't working?  Or perhaps permission is denied or something?

 

but wht when i upload from the mention folder, it work. From other folder, e.g desktop, mypictures.. it won't work..

 

here is the code:

 

<?php include('includes.php');

include_styles();

  $vararray = array('xf_table_name', 'xf_table_sid','xf_table_line','xf_action','xf_readonly', 'xf_ffname1', 'xf_ffval1', 'xf_cbfunc', 'xf_ffname2', 'xf_ffval2');

        $tkf->assign_formvar_request_array($vararray,'','N');

$tnwhere = '';

if (!$tkf->isemptystr($xf_ffname1)) {

$tnwhere .= " and $xf_ffname1 = '$xf_ffval1' ";

}

if (!$tkf->isemptystr($xf_ffname2)) {

$tnwhere .= " and $xf_ffname2 = '$xf_ffval2' ";

}

//show_msg($xf_table_name);

global $tkf;

global $xf_table_name, $xf_table_sid, $xf_table_line, $xf_action;

global $display_filename, $display_filetype, $display_orifilename, $fsize;

global $tnwhere;

 

function display_upload_form()

{ global $tkf;

global $xf_table_name, $xf_table_sid, $xf_table_line, $xf_action;

global $display_filename, $display_filetype, $display_orifilename, $fsize;

global $tnwhere;

 

$vararray = array('xf_table_name', 'xf_table_sid','xf_table_line','xf_action','xf_readonly', 'xf_ffname1', 'xf_ffval1', 'xf_cbfunc', 'xf_ffname2', 'xf_ffval2');

    $tkf->assign_formvar_request_array($vararray,'','N');

//show_msg($display_filename);

 

?>

 

 

    <html>

    <head>

    <title>Asean Kueh Delights</title>

    <style type="text/css" media="screen">

        <!--

        html body

        {background:#fff; font: 76%/1.5em arial, helvetica, sans-serif; color:#333;}

 

        input

        {color:#333;}

        -->

    </style>

    </head>

 

    <body>

 

    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>?xf_table_name=<?php echo $xf_table_name;?>&xf_table_sid=<?php echo $xf_table_sid; ?>&xf_table_line=1&xf_cbfunc=refresh_photo&xf_action='" enctype="multipart/form-data">

 

    <p>Select a file.<br />

    <input type="file" name="myfile" tabindex="1" /></p>

 

    <p><input type="hidden" name="execute" value="1" /></p>

 

    <p><input type="submit" value="Upload File" tabindex="2" />

 

    </form>

 

    </body>

    </html>

 

<?php }

 

// File Upload 

 

function execute_upload()

{

    // root path

    $path = $_SERVER['DOCUMENT_ROOT'];

//show_msg($_SERVER['DOCUMENT_ROOT']);

 

    // upload directory. path will originate from root.

    $dirname = 'temp';

 

    // permission settings for newly created folders

    $chmod = 0700;

 

    // create file vars to make things easier to read.

    $filename = $_FILES['myfile']['name'];

    $filesize = $_FILES['myfile']['size'];

    $filetype = $_FILES['myfile']['type'];

    $file_tmp = $_FILES['myfile']['tmp_name'];

    $file_err = $_FILES['myfile']['error'];

    $file_ext = strrchr($filename, '.');

//show_msg($file_tmp);

    // check if user actually put something in the file input field.

    if (($file_err == 0) && ($filesize != 0))

    {

        // Check extension.

        if (!$file_ext)

        {

            unlink($file_tmp);

            die('File must have an extension.');

        }

 

        // extra check to prevent file attacks.

        if (is_uploaded_file($file_tmp))

        {

            /*

            * check if the directory exists

            * if it doesnt exist, make the directory

            */

            $dir = $path . $dirname;

 

            if (!is_dir($dir))

            {

                $dir = explode('/', $dirname);

 

                foreach ($dir as $sub_dir)

                {

                    $path .= '/' . $sub_dir;

                    if (!is_dir($path))

                    {

                        if (!mkdir($path, $chmod))

                        {

                            unlink($file_tmp);

                            die('<strong>Error:</strong> Directory does not exist and was unable to be created.');

                        }

                    }

                }

            }

 

            /*

            * copy the file from the temporary upload directory

            * to its final detination.

            */

            if (@move_uploaded_file($file_tmp, $dir . '/' . $filename))

            {

//show_msg($file_tmp);

// show_msg($dir . '/' . $filename);

                // success!

                echo "

                <p>Success!</p>

                <p><strong>View File:</strong> <a href=\"$dirname/$filename\">$filename</a></p>

                ";

 

 

success_upload($dirname,$filename,$filesize,$filetype,$file_tmp,$file_err,$file_ext );

//show_msg(\$dirname/$filename\);

//$tkf->redirect2($dirname/$filename\);

            }

            else

            {

                // error moving file. check file permissions.

                unlink($file_tmp);

                echo '<strong>Error:</strong> Unable to move file to designated directory.';

            }

        }

        else

        {

            // file seems suspicious... delete file and error out.

            unlink($file_tmp);

            echo '<strong>Error:</strong> File does not appear to be a valid upload. Could be a file attack.';

        }

    }

    else

    {

        // Kill temp file, if any, and display error.

        if ($file_tmp != '')

        {

            unlink($file_tmp);

        }

 

        switch ($file_err)

        {

            case '0':

                echo 'That is not a valid file. 0 byte length.';

                break;

 

            case '1':

                echo 'This file, at ' . $filesize . ' bytes, exceeds the maximum allowed file size as set in <em>php.ini</em>. '.

                'Please contact your system admin.';

                break;

 

            case '2':

                echo 'This file exceeds the maximum file size specified in your HTML form.';

                break;

 

            case '3':

                echo 'File was only partially uploaded. This could be the result of your connection '.

                'being dropped in the middle of the upload.';

 

            case '4':

                echo 'You did not upload anything... Please go back and select a file to upload.';

                break;

        }

    }

}

 

// Logic Code 

 

if (isset($_POST['execute']))

{

    execute_upload();

}

else

{

    display_upload_form();

}

 

 

function success_upload($dirname,$filename,$filesize,$filetype,$file_tmp,$file_err,$file_ext)

{

global $tkf, $db,$dbl;

global $xf_table_name, $xf_table_sid, $xf_table_line, $xf_action;

global $display_filename, $display_filetype, $display_orifilename, $fsize;

global $tnwhere;

//$uparr = get_upload_file_info2();

//show_msg($uparr);

//if (is_array($uparr))

//{

//$upx = $uparr[0];

//show_msg($filename);

$tfile_size = $filesize; //filesize($upx['tmp_name']);

$tfile_hnd = fopen($filename, 'rb');

 

//show_msg($tfile_hnd);

//show_msg($tfile_size);

$tfile_contents = fread($tfile_hnd, $tfile_size);

 

$outstr = '';

 

$outstr = base64_encode($tfile_contents);

//echo $outstr;

$outstr =  $filetype . "|" . $filename . '|' . $outstr;

//$content = fread($fp, filesize($tmpName));

//$content = addslashes($tfile_contents);

//

//$outstr = str_replace("\n", "", $outstr);

 

$db->executesql("update $xf_table_name set doc_img = '$outstr' where sid = '$xf_table_sid' and line = $xf_table_line $tnwhere");

 

//}

//}

 

if (1 == 1)

{                        // always view

$rst='';

$rcount = $db->getrecordset("select * from $xf_table_name where sid = '$xf_table_sid' and line = $xf_table_line $tnwhere", $rst);

$display_filename='';

if ($rcount > 0)

{

$row = $rst->getrow(0);

if (!$tkf->isemptystr($row['doc_img']))

{

$elements = $tkf->split('|', $row['doc_img']);

//var_dump($elements);

if (!$tkf->isemptystr($elements[0]))

{

$display_filetype = $elements[0];

$display_orifilename = $elements[1];

 

$ts = get_trn_no('ATTPATH');

$tempdir = $tkf->get_temp_dir();

 

mkdir($tempdir . $ts,0755);

 

$tfile_name = $tkf->get_temp_dir() . $ts . "/$display_orifilename";

 

$tfile_hnd = fopen($tfile_name, 'wb');

 

$data_len = $tkf->len($elements[2]);

//$data_len_act = $data_len/2;

 

$outstr = '';

$fsize = $data_len / 2;

$outstr = base64_decode($elements[2]);

//show_msg($elements[0]);

//show_msg($elements[1]);

//show_msg(base64_decode($elements[2]));

fwrite($tfile_hnd, $outstr);

fflush($tfile_hnd);

fclose($tfile_hnd);

 

$display_filename = "/akd/temp/$ts/$display_orifilename";

 

if ($tkf->isemptystr($display_filename))

{

                echo "<tr>";

echo "<td width='100%' class=fldval>";

echo "<p align=center>No document was attached.";

echo "</td>";

echo "</tr>";

        }

        else

{

 

//header("Content-length: $fsize");

//header("Content-type: $display_filetype");

//header("Content-Disposition: attachment; filename=$display_orifilename");

//echo $display_filename;

 

 

echo "<tr>";

echo "<td width='100%' class=fldval><table width='100%' cellpadding=0 cellspacing=1 border=0>";

echo "<tr>";

echo "<td width='20%' class=fldhd bgcolor=lightgrey>File Name:</td>";

echo "<td width='30%' class=fldval bgcolor=gainsboro>$display_orifilename</td>";

echo "<td width='20%' class=fldhd bgcolor=lightgrey>File Size:</td>";

echo "<td width='30%' class=fldval bgcolor=gainsboro>$fsize bytes</td>";

echo "</tr>";

echo "<tr>";

echo "<td width='20%' class=fldhd bgcolor=lightgrey>Type:</td>";

echo "<td width='30%' colspan=3 class=fldval bgcolor=gainsboro>$display_filetype</td>";

echo "</tr></table>";

 

echo "</td>";

echo "</tr>";

echo "<tr height='100%'>";

echo "<td width='100%' class=fldval height='100%'>";

echo "<iframe src='$display_filename' scrolling=yes width='100%' height='100%'></iframe>";

echo "</td>";

echo "</tr>";

 

        }

 

}

 

}

 

}

 

}

 

}

 

?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.