Welcome Guest, Not a member yet? Register   Sign In
upload_success to reflect image w/ email submit
#1

[eluser]oldnews[/eluser]
My project couples an online photo upload with a user submission email form. First, the user uploads a JPEG image restricted to our file size and dimension limts. Then, upon success, the image appears on the upload_success view along with an email form to submit the image concurrent with user info. My image upload method works great, but I've had no joy in passing a variable for $data['file_name'] from the controller to the upload_success view. There's really no need to pass the entire $data array, but, ideally I would also prefer to pass 'image_width' and "image-height' variables to the view and halving their pixel dimensions on the fly (without the need for an adjunct folder to store those transitory downsized copies). Another issue, which I plan to tackle once I get THIS to work, is extending a class Form in the controller for the upload_success view which is already powered by class Upload. Then, users could submit, in step 2, text fields about themselves and their photo (with validation) and the image auto-attached.

I am using the recommended templates and methods supplied in the CI user_guide. Here are coding excerpts with 3 failed methods to pass the file_name variable commented out from the end of the upload.php controller and the entire upload_success view which yields an error for undefined variable image. Any guidance that can help me get on the right track would be immensely appreciated.

Code:
else
        {

//            $image = $this->upload->data();
//            $data['image'] = "./uploads/".$image['file_name'];
//            $image = $data['image'];

//            $image = $this->upload->data['file_name'];

//            $image = $data['file_name'];

            $data = array('upload_data' => $this->upload->data());

            $this->load->view('upload_success', $data);
        }
    }    
}
?>

Code:
<head>
<title>Upload Success</title>
</head>
<body>

<h3>Your photo was successfully uploaded!</h3>

<h1>&lt;?php echo $image;?&gt;</h1>

<ul>
&lt;?php foreach($upload_data as $item => $value):?&gt;
<li>&lt;?php echo $item;?&gt;: &lt;?php echo $value;?&gt;</li>
&lt;?php endforeach; ?&gt;
</ul>

&lt;/body&gt;
&lt;/html&gt;
#2

[eluser]LuckyFella73[/eluser]
did you try something like this:
Code:
$data['upload_data'] = $this->upload->data();
$this->load->view('upload_success', $data);
#3

[eluser]oldnews[/eluser]
Thanks. Actually, to avoid possible duplicate filenames in my upload folder, libraries/Upload.php modifies user's filename by appending a 1, 2, 3, et cetera, so I must pass the associated (possibly modified) $data['file_name'] to assure the correct photo is reflected, and attached in the subsequent email submission
#4

[eluser]oldnews[/eluser]
I tried LuckyFella73's code substitution in the Upload controller and it performed exactly as does:
Code:
$data = array('upload_data' => $this->upload->data());
What I am attempting to do is to extract the variable $value associated with the item 'file_name' (unmodified or modified by libraries/upload) from the upload_data array so that I can reflect it as an embedded image in the upload_success view. So, this afternoon, I've been trying short PHP code snippets within the upload_success view, instead of hammering away at the upload controller file. I sense I am close, but still am getting "undefined variable: item" and "undefined index" errors on this upload_success page:
Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Upload Success&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

<h3>Your photo was successfully uploaded!</h3>

&lt;?php if($upload_data[$item] == 'file_name') :?&gt;
filename: &lt;?php echo $value; ?&gt;<br />
&lt;?php endif; ?&gt;
&nbsp;<br />

<ul>
&lt;?php foreach($upload_data as $item => $value):?&gt;
<li>&lt;?php echo $item;?&gt;: &lt;?php echo $value;?&gt;</li>
&lt;?php endforeach; ?&gt;
</ul>

<br />
&nbsp;<br />

&lt;/body&gt;
&lt;/html&gt;
It's particularly frustrating since the same variable and index work fine in the boilerplate $data array supplied in the CI user-guide. So, where am I going wrong in my coding? Any suggestions? Thanks.
uploadpixmaverik.com
#5

[eluser]oldnews[/eluser]
I'm coming to the conclusion that what I want to accomplish cannot be accomplished given the tools I have to work with. I have read the user-guide and thought, perhaps loading the parser library might yield the clean echo of a single chosen value from the upload data array using {} braces surrounding the desired value. But the parser->parse, when uncommented in the controller, only duplicated what load->view does, i.e., run the entire data dump, and the braces are returned as plain html text. The other possible solution attempted in the view to echo the desired value as a stand-alone yielded a "Trying to get property of non-object" error. Yes, I am trying to get the property value of a data array constructed by a CI boilerplate library for uploaded images. If this is nothing more than an exercise in futility, then I suppose it explains why there's no workable solution to reflect the user's uploaded picture in an upload_success view.

upload controller (end game):
Code:
else
        {

//            $this->load->library('parser');

            $data = array('upload_data' => $this->upload->data());

//            $this->parser->parse('upload_success', $data);

            $this->load->view('upload_success', $data);
        }
    }    
}
?&gt;

upload_success view:
Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Upload Success&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

<h3>Your photo was successfully uploaded!</h3>

filename: {file_name}<br />
&nbsp;<br />

&lt;?php echo $upload_data['file_name'] -> $file_name;?&gt;<br />

<ul>
&lt;?php foreach($upload_data as $item => $value):?&gt;
<li>&lt;?php echo $item;?&gt;: &lt;?php echo $value;?&gt;</li>
&lt;?php endforeach; ?&gt;
</ul>

<br />
&nbsp;<br />

&lt;/body&gt;
&lt;/html&gt;

UploadPixMaverik
#6

[eluser]LuckyFella73[/eluser]
Maybe I didn'd understand what you were trying to do - sorry for that.

If I get you right (this time) you want the just uploaded image shown
on the succes page. If thats the case you could do it this way:

Controller:
Code:
&lt;?php
$upload_data = $this->upload->data();
$uploaded_file = $upload_data['full_path']; // full path including filename

$data['image'] = $uploaded_file;
$data['width'] = $upload_data['image_width'];
$data['height'] = $upload_data['image_height'];
$data['file_name'] = $upload_data['file_name'];

$this->load->view('upload_success', $data);
?&gt;

Success page:
Code:
&lt;head&gt;
&lt;title&gt;Upload Success&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

<h3>Your photo was successfully uploaded!</h3>

<img src="&lt;?php echo $image;?&gt;" width="&lt;?php echo $width;?&gt;" height="&lt;?php echo $height;?&gt;" border="0" alt="&lt;?php echo $file_name;?&gt;">


&lt;/body&gt;
&lt;/html&gt;
#7

[eluser]oldnews[/eluser]
LuckyFella73,

Thank you. Code works great; elegant solution!




Theme © iAndrew 2016 - Forum software by © MyBB