954,597 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Can't write to csv?

Hi there, I'm using php to try and take two separate arrays ($colors and $message), write them to a single multidimensional array ($endline), and then write the resulting md array into a csv. This works fine on my localhost, but for some reason it doesn't work through my web host.

Could this be a windows unix difference? I've had some minor problems with this before.

It also may be a problem with the way the webhost runs sessions. The $filename below is the previous session file from which I've gathered the $colors array. I just wanted to write over it for efficiency's sake.

Here's the key snippet. Is there any way I could recode to make it more workable?

foreach (range (0,9) as $r)
	{
	if ($colors[$r] != "")
		{
		$endline[$r] = array($colors[$r],$message[$r]);
		}
	}



$fp = fopen($filename, 'w');

foreach ($endline as $item)

	{
	fputcsv($fp, $item);
	}

fclose($fp);
elbeato
Light Poster
31 posts since Jun 2005
Reputation Points: 10
Solved Threads: 3
 

Check if you are allowed to write into your folder. Most likely you can change this in your cPanel or via your FTP client.

pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 
Check if you are allowed to write into your folder. Most likely you can change this in your cPanel or via your FTP client.

I definitely think you're on the right track. The error logs are suggesting that the temporary session file I'm trying to read to create $colors can't be found. It gives the error "fgetcsv() expects parameter 1 to be resource," and then repeats the error as it loops through, trying to read the color strings.

I've used cPanel to grant all permissions to the folder where the temp files are stored, but to no avail. Any other ideas?

elbeato
Light Poster
31 posts since Jun 2005
Reputation Points: 10
Solved Threads: 3
 

Perhaps your filename is wrong. Storing it in the temp dir on linux is not a good idea. You best create your own temporary folder, relative to your document root, and store the files there.

pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 
Perhaps your filename is wrong. Storing it in the temp dir on linux is not a good idea. You best create your own temporary folder, relative to your document root, and store the files there.

Thanks for your further suggestions.

I changed the temp folder and it's still not finding the file properly.

I did notice something very odd. To open up the session on the page, I use the following code

session_start();
$filename = $_SESSION['message'];


$filename is the same variable name I use on the previous page to identify the session.

Now, here's the weird part. If I change the variable $filename in this snippet to another name, the session screws up on my localhost. Too weird. Probably a noob error. This session stuff is pretty new to me. Is $filename being set as a global variable somehow? Why can't I assign a new variable name to $_SESSION('message']?

elbeato
Light Poster
31 posts since Jun 2005
Reputation Points: 10
Solved Threads: 3
 

It turns out it was a session problem, after all. I had to add a session_start() and session_destroy() to the beginning of my code, just to clear out anything that was kicking around.

The app's still glitchy because php runs so differently on my server vs. localhost, but this specific issue of writing to a csv file is solved.

Thanks for the help, pirateas. I wouldn't have got on the right track without your suggestions.

elbeato
Light Poster
31 posts since Jun 2005
Reputation Points: 10
Solved Threads: 3
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: