Jump to content

How to get the name of a file upload?


ninevolt1

Recommended Posts

I have a form which I downloaded which I am butchering up. It works great except I cannot figure out how to do one thing. Basically I am using this form to upload jpegs to a web site. In turn this PHP script takes that form and inserts the image name onto an html page so I can download the image at later date. Down near the bottom of all this code is the form.

 

This line uploads my image:

<tr><td align="right" class="copy"> Upload </td><td valign="middle"><input name="image_file" type="file" class="copy" size="40"></td></tr> 

 

How can I make the form/php script recognize the name of the file that is uploaded so I can use it to make an automatic link? Right now the only way I have been able to do it is to manually type the name of the image I am uploading onto a separate line in the form so that my PHP script knows to use that as the link for me to click on later to download the image. The only thing is it is time consuming and prone to typos/errors since some of the files that I upload have long and weird names.

 

 

 


<html>
<link href="../clientstyle.css" rel="stylesheet" type="text/css">
<body>

<table width="900"  align="center" border="0" cellpadding="5">
  <tr>
    <td width="200" height="50" align="right" class="copy"></td><td align="left" valign="bottom"><span class="client">Upload Proofs</span></td>
  </tr>
</table>
<?
        include ("template.inc");

    $summary = $_POST[summary];
$passwd = $_POST[passwd];
$date = $_POST[date];
$pubdate = date('D, d M Y H:i:s T');
$body = $_POST[body];
$price = $_POST[price];
$link = $_POST[link];
$linkname = $_POST[linkname];
$image = $_POST[image];
$article_id = $_POST[article_id];
$article_url = $link;


#foreach($GLOBALS as $a => $b){	print "<li>$a => $b";}

$summary_template4 = "wowt_summary4.html";

function summary_page ($date, $summary, $body, $linkname, $image, $article_id, $article_url, $link)
{


global $summary_template;
        	$t = new Template();
        	$t->set_file("SummaryPage", $summary_template);
	$article_url = $link;
	$image_url = $image;
	$date = nl2br($date);
	$summary =  nl2br($summary);	 
	$t->set_var( array(
			"date"    => $date,
			"summary" => $summary,
			"body" => $body,
			"linkname" => $linkname,
			"image" => $image,
			"article_id"    => $article_id,
			"article_url"    => $article_url,
			"link"    => $link,
			));
	$t->parse("Summary", "SummaryPage");
	return $t->get_var("Summary");
}






function summary_page4 ($date, $summary, $body, $linkname, $image, $article_id, $article_url, $link)
{
    global $summary_template4;
        	$t = new Template();
        	$t->set_file("SummaryPage4", $summary_template4);
	$article_url = $link;
	$image_url = $image;
	$date = nl2br($date);
	$summary =  nl2br($summary);	 
	$t->set_var( array(
			"summary" => $summary,
			"body" => $body,
			"linkname" => $linkname,
			"image" => $image,
			"article_id"    => $article_id,
			"article_url"    => $article_url,
			"link"    => $link,
			));
	$t->parse("Summary4", "SummaryPage4");
	return $t->get_var("Summary4");
}




function main_page ($date, $summary, $body, $linkname, $image, $article_id, $article_url, $link)
{
	global $article_template;

                $t = new Template();
                $t->set_file("ArticlePage", $article_template);
                $article_url = $link;
			$image_url = $image;
                $date = nl2br($date);
                $summary =  nl2br($summary);
                $body =  nl2br($body);
			$t->set_var( array(
                                "date"    => $date,
							"summary" => $summary,
							"body" => $body,
							"linkname" => $linkname,
							"image" => $image,
							"article_id"    => $article_id,
							"article_url"    => $article_url,
							"link"    => $link,
                                ));
                $t->parse("Article", "ArticlePage");
                return $t->get_var("Article"); 
}

function add_article($filename, $news)
{

	if(file_exists($filename)){
		$fh = fopen($filename, "r");
		$old_news = fread($fh, filesize($filename));
		fclose($fh); 
	}

	/* TODO: Multipage articles
		preg_match_all("<!--ARTICLE PAGE=(\d*)-->", $old_news, $matches;

		if( count($matches[0]) >= $max_summary){
			$oldfilename = $filename.($matches[0][0]+1);
		} 
	*/

	$fh = fopen($filename, "w");
	$news = stripslashes($news);
	fwrite($fh, "$news $old_news");
	fclose($fh);
}

if(strcmp($linkname, "")){	
	if(!(strcmp($passwd, "test"))){	

		add_article("wow_summary4.html", summary_page4($date, $summary, $body, $linkname, $image, $article_id, $article_url, $link));

		echo "<p> <a href=index.php>File</a> has been uploaded.<p>";
	}else{
		echo "<p><b> Password incorrect! </b>";
	}


}
?>
<?
//print_r($_POST);

if($_POST["submit"] == "Add")
{
unset($imagename);

if(!isset($_FILES) && isset($HTTP_POST_FILES))
$_FILES = $HTTP_POST_FILES;

if(!isset($_FILES['image_file']))
$error["image_file"] = "An image was not found.";


$imagename = basename($_FILES['image_file']['name']);
//echo $imagename;

if(empty($imagename))
$error["imagename"] = "The image was not found.";

if(empty($error))
{
$newimage = "" . $imagename;
//echo $newimage;
$result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage);
if(empty($result))
$error["result"] = "There was an error moving the uploaded file.";
}

}

?>

<form action="<?$_SERVER["PHP_SELF"];?>" enctype="multipart/form-data" method=post>
<table width="900" border=0 align="center" cellpadding="5">
<tr> <td width="200" align="right" class="copy"> Article ID: </td><td> <input type=text name=article_id value=<? echo date("Y_m_j_is"); ?> class="copy2" size=30></td></tr>
<tr> <td align="right" class="copy"> Upload </td><td valign="middle"><input name="image_file" type="file" class="copy" size="40"></td></tr>
<tr> <td align="right" class="copy"> Link </td><td><input name=image type=text class="copy2" size=40></td></tr>
<tr> <td align="right" class="copy"> Link Name</td><td><input name=linkname type=text class="copy2" size=40></td></tr>
<tr> <td align="right" class="copy"> Password </td><td> <input name=passwd type=password class="copy2" size=20>
      
  <input name=submit type=submit class="deleoinputter" value=Add></td></tr>
</table>
</form>


<table width="900" border="0" align="center" cellpadding="5">
  <tr>
    <td height="20" width="200"></td><td align="left" valign="bottom" class="copy">Form last updated: <!-- #BeginDate format:fcAm1a -->Monday, March 15, 2010 4:30 PM<!-- #EndDate --></td>
  </tr>
</table>

<?
if(is_array($error))
{
while(list($key, $val) = each($error))
{
echo $val;
echo "<br>\n";
}
}
?>

Link to comment
Share on other sites

Thanks for the reply. How can I get that info into the array up top? If you haven't already noticed, I'm not the most proficient guy here!

 

 

This part of the code pretty much defines all my values which get inserted into my HTML document:

    $summary = $_POST[summary];
$passwd = $_POST[passwd];
$date = $_POST[date];
$pubdate = date('D, d M Y H:i:s T');
$body = $_POST[body];
$price = $_POST[price];
$link = $_POST[link];
$linkname = $_POST[linkname];
$image = $_POST[image];
$article_id = $_POST[article_id];
$article_url = $link;

 

So how can I get the value from the line you pointed out into that section? Basically this line:

 

	$image = $_POST[image];

 

is where my HTML document will be looking for the name of the file. I supposed I need to replace the "$_POST[image];" part of that equation with something to get the value from the line you pointed me to. I tried replacing it with "$imagename;" but it didn't work. Any ideas?

Link to comment
Share on other sites

If you try the following, you're not getting the name of the image?

 

$image = basename($_FILES['image_file']['name']);

 

Since you admit that you are not "the most proficient guy here" you should start reading and try to understand what you are doing and not just copying and pasting code without understanding why you are using it.. :)

Link to comment
Share on other sites

That did it! Thanks that is what I was looking for. I'm usually able to cobble together what I need to do these little projects. Every once in a while I get stuck on something. I wish I had time to completely learn PHP but I don't have the time and if I did and I don't think I would ever learn to use 99% of what I learned. It would be like going to school to completely learn how to rebuild a car engine when all I need to do is change the spark plugs! Thanks again for your help!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.