Jump to content

[SOLVED] uploaded file not saving


shergold

Recommended Posts

Hello again, for some reason the uploaded file is not being saved in the specified directory, the code with the move_uploaded_file() function in is being executed. I appriciate any help.

 

<?php
include("config.inc.php");
session_start();
if (!isset($_SESSION['user'])){
header("location: login.php");
exit();
}

//filepath
$filename = $_FILES['file']['name'];
$filename = explode(".",$filename);
$filename = $filename[0];
$code = uniqid();
$newname = $filename . $code;
$domain = $_SERVER['HTTP_HOST'];
$link = "$domain/image.php?loc=$newname";
$targetpath = $_SERVER['HTTP_HOST'] . "/imageup/";
$targetpath = $targetpath . $newname;

//file restrictions	
if (
$_FILES['file']['type'] == "image/jpg"
|| $_FILES['file']['type'] == "image/gif"
|| $_FILES['file']['type'] == "image/jpeg"
&& $_FILES['file']['size'] < 1048576
)
{
if($_FILES['file']['error'] > 1)
	{
	exit ("<center>There has been an error uploading your file:" . $_FILES['file']['error'] . "</center><br />");
	}
	elseif (file_exists($targetpath))
	{
	echo "<center>A file with this name already exists.</center>";
	}
	else
	{
	echo basename($_FILES['file']['name']);
	$filesize = $_FILES['file']['size']/1000;
	$filesize = number_format($filesize,0);
	move_uploaded_file($_FILES['files']['tmp_name'],$targetpath);
	echo "<center>Your file has been succesfully uploaded</center><br />";
	echo "<div class=\"upload_info\" align=\"center\">";
	echo "<center>File uploaded: " . $_FILES['file']['name'] . "<br />";
	echo "File type: " . $_FILES['file']['type'] . "<br />";
	echo "File size: " . $filesize . " KB<br />";
	echo "Storage location: " . "<a href=\"$link\">$link</a>" . "<br />";
	echo "expiry date: ";
	echo "</div>";
	}

}
else
echo "<center><b>Invalid file</b></center>";


?>

 

Thanks,

Shergold.

Link to comment
https://forums.phpfreaks.com/topic/166065-solved-uploaded-file-not-saving/
Share on other sites

thanks allot, but now im getting the following errors:

 

Warning: move_uploaded_file(localhost/imageup/05-03-09_16174a5ded9cc12cb) [function.move-uploaded-file]: failed to open stream: No such file or directory in C:\xampp\htdocs\imgprocess.php on line 41

 

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\xampp\tmp\php2A8F.tmp' to 'localhost/imageup/05-03-09_16174a5ded9cc12cb' in C:\xampp\htdocs\imgprocess.php on line 41

 

The imageup directory is definatly there.

 

Thanks again,

shergold.

 

 

 

The path 'localhost/imageup/05-03-09_16174a5ded9cc12cb' is incomplete or incorrect.

 

Use getcwd() to get the current working directory (http://us.php.net/getcwd) and then use the complete path this outputs.

 

Also, it doesn't look like your script is outputting a valid filenname, 'localhost/imageup/05-03-09_16174a5ded9cc12cb' should actually have read something like ''localhost/imageup/file_name.txt'

I fixed it so that it outputs a valid file name but i am still getting errors, this is my new code:

 

<?php
include("config.inc.php");
session_start();
if (!isset($_SESSION['user'])){
header("location: login.php");
exit();
}

//filepath
$filename = $_FILES['file']['name'];
$filename = explode(".",$filename);
$fileid = uniqid();
$fileurl = $_SERVER['HTTP_HOST'] . "/image.php?loc=" . $fileid . $filename[0];
$filepath = $_SERVER['HTTP_HOST'] . "/imageup/" . $fileid . $filename[0] . "." . $filename[1];

//file restrictions	
if (
$_FILES['file']['type'] == "image/jpg"
|| $_FILES['file']['type'] == "image/gif"
|| $_FILES['file']['type'] == "image/jpeg"
&& $_FILES['file']['size'] < 1048576
)
{
if($_FILES['file']['error'] > 1)
	{
	exit ("<center>There has been an error uploading your file:" . $_FILES['file']['error'] . "</center><br />");
	}
	elseif (file_exists($filepath))
	{
	echo "<center>A file with this name already exists.</center>";
	}
	else
	{
	echo $filename;
	$filesize = $_FILES['file']['size']/1000;
	$filesize = number_format($filesize,0);
	move_uploaded_file($_FILES['file']['tmp_name'],$filepath);
	echo "<center>Your file has been succesfully uploaded</center><br />";
	echo "<div class=\"upload_info\" align=\"center\">";
	echo "<center>File uploaded: " . $_FILES['file']['name'] . "<br />";
	echo "File type: " . $_FILES['file']['type'] . "<br />";
	echo "File size: " . $filesize . " KB<br />";
	echo "Storage location: " . "<a href=\"$fileurl\">$fileurl</a>" . "<br />";
	echo "expiry date: ";
	echo "</div>";
	}

}
else
echo "<center><b>Invalid file</b></center>";

?>

 

I dont understand why?

 

Thanks again,

shergold.

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.