Jump to content

[SOLVED] Upload file problem


westen

Recommended Posts

I am trying to implement a very simple photo upload.  For some reason $_FILES['userfile']['tmp_name'] does not exist, could anyone tell me why? Here is the php:

[pre]

        if(isset($_FILES['uploadedfile'])) {

$target_path = "./uploads/";

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

 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {

    echo "The file ".  basename( $_FILES['uploadedfile']['name']).

    " has been uploaded";

} else{

    echo "There was an error uploading the file, please try again!";

    echo ("<p>file name: " . $_FILES['uploadedfile']['name']);

    echo ("<p>temp name: " . $_FILES['uploadedfile']['tmp_name']);

    echo ("<p>target path: " . $target_path);

}

}

[/pre]

 

It outputs:

 

There was an error uploading the file, please try again!

 

file name: photo.jpg

 

temp name:

 

target path: ./uploads/photo.jpg

 

 

Any help would be much appreciated.  I am new to php and this has me baffled.

 

Cheers,

Westen

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

could be write permissions

 

<?php
         if(isset($_FILES['uploadedfile'])) {
		$target_path = "./uploads/";
		$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
		//Add the line below
		echo (!is_writable($target_path))?"Cannot Write file, Check Permission":"";
		if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
		    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
		    " has been uploaded";
		} else{
		    echo "There was an error uploading the file, please try again!";
		    echo ("<p>file name: " . $_FILES['uploadedfile']['name']); 
		    echo ("<p>temp name: " . $_FILES['uploadedfile']['tmp_name']); 
		    echo ("<p>target path: " . $target_path);
		}
	}
?>

 

My form code looks like this.  What would I have to change th permissions on if that is the problem.  Apologies for my ignorance.

 

<form action="register.php"  enctype="multipart/form-data" method="POST" class="register">
<p><br/></p>
<label for="username" class="textb">Username:</label>
<input type="text" name="username" size="20" maxlength="10" value="<?php echo $username;?>"/></p>

<label for="name" class="textb">Name: </label>
<input type="text" class="textb" name="name" size="20" maxlength="20" value="<?php echo $name;?>"/></p>

<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<label for="upload" class="textb">Upload Photo: </label>
<input type="file" name="uploadedfile"/></p>

<label for="password" class="textb">Password: </label>
<input type="password" name="password" size="20" maxlength="10" value="<?php echo $password;?>"/></p>

<label for="confirmpassword" class="textb">Confirm Password: </label>
<input type="password" name="confirmpassword" size="20" maxlength="10" value="<?php echo $confirmpassword;?>"/></p>

    <input type="hidden" name="submitted" value="TRUE"/></p>

<div id="standard">
	<input type="submit" name="submit" value="Create Account"/></p>
</div>
</form>

New Upload Code:

 

<?PHP
if(isset($_FILES['uploadedfile'])) {
$target_path = "./uploads/";
$name = $_FILES['uploadedfile']['name'];
$target_path = $target_path . $name;
$EXP = explode(".",$name);

//Set your own file types here
$valid_file_types = array('jpg','jpeg','bmp','gif','png','mp3');

$i = 0;
$a = count($valid_file_types);

$type = strtolower($EXP[($a -1)]);

//Start Validation Check
while($i<$a){
	if($valid_file_types[$i] == $type){
		$VALID = 1;
	}else{
		$VALID = 0;
	}
	$i++
} 

//Check whether file is valid or not
if($VALID != 1){
	echo('File: '.$name.' is an invalid file type.');
	exit;
}

//Check whether you can copy the file or not.
if(copy($_FILES['uploadedfile']['tmp_name'], $target_path)) {
	echo "The file ". $name." has been uploaded.";
}else{
	echo "There was an error uploading the file, please try again!";
	echo ("<p>file name: " . $'name); 
	echo ("<p>temp name: " . $_FILES['file']['tmp_name']); 
	echo ("<p>target path: " . $target_path);
}
?>

 

REMEMBER CHANGE THE $valid_file_types TO WHATEVER YOU WANT!!!

 

Code UNTESTED

Yes I did, thank you. I do not have permission to write to file.  The uploads folder lists 0644 under permissions (this is a folder I created and it started out as 0755 but I changed it to be the same as the others which I assumed was writable).  I do not even know what this is or should be.  This is the first site I have tried to set up.

 

Cheers,

Westen

Apologies, I just changed it back and it works fine.  It must have been a different problem that made me change it in the first place.  Sorry for wasting your time.  Maybe I will get around to answering some stupid questions when I know more to pay back the stupidity.

 

Cheers,

Nicholas

Now I want to cry, it gave me the upload successful message once (but the uploaded file was empty), I didn't change anything and now it is saying I do not have permission again.  I am really confused.  From what I can tell 0755 is what I want on my directory isn't it?  My php files are in /public_html and my upload folder is /public_html/uploads, I cannot see what I am doing wrong.

 

Thank you,

NT

try

		$target_path = "./uploads/";
		$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

to

		$target_path = "./uploads/";
		$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
		echo "~$target_path~";

 

to see the path and check its correct

 

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.