I have this script that will NOT work no matter what i add to it, i have fopen not working and i believe that is the root of all my other errors but i cannot be sure, i originally had 8 errors but 4 of them were simple notices for undefined variables. I want to make my code work cross platform because I feel my program should be freely used on multiple platoforms, be it mac os, windows, or linux (preferably linux), any help would be greatly appreciated. Here are the errors and code below.

errors

Warning: fopen(/uploaded/606.txt): failed to open stream: No such file or directory in /opt/lampp/htdocs/upload/newup.php on line 19

Warning: fgetcsv() expects parameter 1 to be resource, boolean given in /opt/lampp/htdocs/upload/newup.php on line 24

Warning: Invalid argument supplied for foreach() in /opt/lampp/htdocs/upload/newup.php on line 26

Notice: Undefined variable: query in /opt/lampp/htdocs/upload/newup.php on line 39
Query was empty

and here is the code im working with.

<?php
ini_set('session.save_path', '/opt/lampp/htdocs/tmp');;
session_start();
require ('dbconnect.php'); // connect to database

// initial database stuff
    set_time_limit(0);
    ini_set('memory_limit', '1024M');
    $file = NULL;

    // Where the file is going to be placed 
    $target_path = "/uploaded/";

    /* Add the original filename to our target path.  
Result is "uploads/filename.extension" */

    $file = $file.basename($_FILES['file']['name']);

    $file_handle = fopen($target_path.$file, "r");

    $count= 1;
    mysql_query("TRUNCATE TABLE numdata") or die("MySQL Error: " . mysql_error()); //Delete the existing rows

    while (($data = fgetcsv($file_handle, 0, ",")) !== FALSE)
    {
        foreach($data as $row)
        {
            if($count % 2)
            {
                $complete = $row;
            }
            else{
                $complete .= $row;
                $insertArray[] = $complete;
            }
        $count++;
        $query="INSERT INTO numdata(numb) values($complete)";
        }
        mysql_query($query) or die(mysql_error());
    }

fclose($file_handle);

also forgot to mention that i have this script working FLAWLESSLY on windows, so yeah :/

Recommended Answers

All 10 Replies

Platform shouldn't make a difference. These are PHP errors, and will show up no matter what platform you are running on.

Are you CERTAIN the file /uploaded/606.txt exixts?

And it might be worth trying this: $target_path = "uploaded/";

Because it is common practice to use ./ or simply nothing, because all this does is refers to the directory your PHP file is in. When looking for other files, PHP (and any other language) always starts in the same location as what the PHP file is in. So therefore there's no need to tell it where to start looking from if it already knows - if you get what I mean?

u see i figured the same thing, i already tried this and nothing happened, i looked further into this and was trying to maybe save the uploaded file somewhere forcefully so make it work but that did nothing, im not sure why this is happeneing, but it might have something to do with the php version, my windows machine is running an older version, where my linux box is running 5.2 was the fopen function changed in some way cause im not finding much on it over the net?? whats up?

Why don't you use file_get_contents() instead if you're only reading the file?

This should produce pretty much the same result, but doesn't worry about handling files etc.

I really can't imagine it's anything to do with the platforms - but maybe version. file_get_contents() will work on v4.3+, but realistically you should ALWAYS use v5, because they release a new version for a reason. Normally it's things like security updates and new functions, which both are vital to the protection of your script but can also enhance it (either way pretty worthwhile).

OKAY we are making progress and thats wonderful but now im stuck with a new error, now the fgetcsv() is coming back with an error still. Only this time the error has changed from boolean to string, is there another way i need to use it or another function all together? here is what i have so far.

    $file = NULL;

    // Where the file is going to be placed 
    $target_path = "uploaded/";

    /* Add the original filename to our target path.  
Result is "uploads/filename.extension" */

    $file = $file.basename($_FILES['file']['name']);

    $file_handle = file_get_contents($target_path, $file);

    $count= 1;
    mysql_query("TRUNCATE TABLE numdata") or die("MySQL Error: " . mysql_error()); //Delete the existing rows

    while (($data = fgetcsv($file_handle, 0, ",")) !== FALSE)
    {
        foreach($data as $row)
        {
            if($count % 2)
            {
                $complete = $row;
            }
            else{
                $complete .= $row;
                $insertArray[] = $complete;
            }
        $count++;
        $query="INSERT INTO numdata(numb) values($complete)";
        }
        mysql_query($query) or die(mysql_error());
    }

fclose($file_handle);

maybe im using it wrong now since i have changed the method im getting the contents of the file im uploading?

I'm sorry I've made a mistake and told you wrong! You were right with fopen()!

In the documentation, it states that in fgetcsv(), the length parameter became optional in v5. In v5.1.0+, you can set it to "0", which means unlimited.

If your PHP version is less than v5.1.0, you must set the length parameter must be greater than the longest line (in characters) to be found in the CSV file. This could well be what is causing your problem.

Sorry for misleading you there!

okay so i have tinkered and came across something very interesting. and please dont feel bad for a little mistake believe me i have made my fair share of them as well so no big deal at all.

Now for the program, i messed with the fgetcsv() a little and found that it DOES INDEED work but ONLY if the file im trying to upload is IN the same directory as my php file. This is obviously a problem if the computer is pc2454 out of 5000 and they wish to upload it from their pc right there, they wont be able to physically put it in the directory itself. Here is the magic though, you see on windows this isnt a problem and uploads from another pc just fine, has been tested and deployed with over ten different machines and none of which had any issues. So why is it that this doesnt work on linux? This just raises more questions lol but such is the programming lifestyle!

here is my code that is technically working but not quite like id wish it to work.

$file = NULL;

    // Where the file is going to be placed 
    $target_path = "uploaded/";

    /* Add the original filename to our target path.  
Result is "uploads/filename.extension" */

    $file = $file.basename($_FILES['file']['name']);

    $file_handle = fopen($file, "r");

    $count= 1;
    mysql_query("TRUNCATE TABLE numdata") or die("MySQL Error: " . mysql_error()); //Delete the existing rows

    while (($data = fgetcsv($file_handle, 0, ",")) !== FALSE)
    {
        foreach($data as $row)
        {
            if($count % 2)
            {
                $complete = $row;
            }
            else{
                $complete .= $row;
                $insertArray[] = $complete;
            }
        $count++;
        $query="INSERT INTO numdata(numb) values($complete)";
        }
        mysql_query($query) or die(mysql_error());
    }

fclose($file_handle);

i dont know i this is just a repeat paste of code but this works just not the same way it does on windows which is SO WEIRD lol.

I just cannot figure this issue out, i dont understand how the platform has anything to do with this code working? cause i tested my new code out on windows and out of curiosty i used the exact SAME xampp install to be sure it wasnt that but to my suprise i got the same results, it wont work on linux but it will in windows? why is this, is there something special i have to do in linux to get around this problem?

when uploading a file through a file input
you'd access the actual file through its "uploaded" or temp name

$file = $_FILES["file"]["tmp_name"];
$file_handle = fopen($file, "r");

you sir have helped me get my answer working right, i see my issue now, i was accessing the files directly while looping through them, they couldnt be accessed because they werent in the same directoy but when i changed it to the temp files it worked like a charm!

TYSM!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.