Reading file contents from temp

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PnHoPob
Forum Newbie
Posts: 16
Joined: Thu Nov 10, 2005 7:16 pm

Reading file contents from temp

Post by PnHoPob »

Hello all!

I'm trying to save the contents of a file in its submitted temporary location to a string.
Here's what my code looks like:

Code: Select all

<?php

/* In my HTML
<form action="upload.php" method="post" enctype="multipart/form-data">
	<label for="file"></label>
	<p align="center">
		<input type="file" name="file" id="file">
		<br>
		<input type="submit" name="submit" value="Submit File"><br>
		<b><font face="Arial" size="1">might not be working yet</font></b>
	</p>
  </form>
*/

	print_r($_FILES['file']);

	$filename = $_FILES['file']['name'];
	$fileloc = $_FILES['file']['tmp_name'];
	$newloc = '/var/www/users/myserver/public_html/files/';
	
	chmod($fileloc,777);
	
	$string = file_get_contents($fileloc);

	//check to see if file was read from tmp
	if ($string == "")
	{
	$string = "[No Contents]\nError reading $filename from its temporary location.\n";
	}


	//move file
	if (move_uploaded_file($fileloc,$newloc.$filename))
	{
	$string = "File succesfully moved\n".$string;
	}

	else {
	$string = $string."Error moving $filename from $fileloc\n";}
	
	//check to see if file was read from new location
	$oldstring = $string;

	$string2 = file_get_contents($newloc);
	$string = $string2.$string;

	if($oldstring == $string)
	{
	$string = $string."Error reading $filename from its new location. You're truly out of luck...";
	}
	
    include('output.php');
?>
Here's what I get when I try to upload test.txt:

Code: Select all

[No Contents]
Error reading FILE.txt from its temporary location.
Error moving FILE.txt from /tmp/phplsBFf30838
Error reading FILE.txt from its new location. You're truly out of luck...
Anyone know what's wrong?
Last edited by PnHoPob on Tue Nov 29, 2005 2:03 am, edited 7 times in total.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

You're trying to read the file in using 'name' right off the bat, to read the file into a string use ['tmp_name']. And file_get_contents is the function you want, unless you need it in an array for some reason.
PnHoPob
Forum Newbie
Posts: 16
Joined: Thu Nov 10, 2005 7:16 pm

Post by PnHoPob »

ooh, I see...

Okay, I've completely reworked everything. However, it still doesn't save the file on tmp to a string... or move it... or read it from the new location.
::clueless::


Any ideas?
Post Reply